2 * OOjs UI v0.1.0-pre (f9c217dfa4)
3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2014 OOjs Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2014-09-10T17:25:40Z
16 * Namespace for all classes, static methods and static properties.
48 * Get the user's language and any fallback languages.
50 * These language codes are used to localize user interface elements in the user's language.
52 * In environments that provide a localization system, this function should be overridden to
53 * return the user's language(s). The default implementation returns English (en) only.
55 * @return {string[]} Language codes, in descending order of priority
57 OO
.ui
.getUserLanguages = function () {
62 * Get a value in an object keyed by language code.
64 * @param {Object.<string,Mixed>} obj Object keyed by language code
65 * @param {string|null} [lang] Language code, if omitted or null defaults to any user language
66 * @param {string} [fallback] Fallback code, used if no matching language can be found
67 * @return {Mixed} Local value
69 OO
.ui
.getLocalValue = function ( obj
, lang
, fallback
) {
76 // Known user language
77 langs
= OO
.ui
.getUserLanguages();
78 for ( i
= 0, len
= langs
.length
; i
< len
; i
++ ) {
85 if ( obj
[fallback
] ) {
88 // First existing language
98 * Message store for the default implementation of OO.ui.msg
100 * Environments that provide a localization system should not use this, but should override
101 * OO.ui.msg altogether.
106 // Tool tip for a button that moves items in a list down one place
107 'ooui-outline-control-move-down': 'Move item down',
108 // Tool tip for a button that moves items in a list up one place
109 'ooui-outline-control-move-up': 'Move item up',
110 // Tool tip for a button that removes items from a list
111 'ooui-outline-control-remove': 'Remove item',
112 // Label for the toolbar group that contains a list of all other available tools
113 'ooui-toolbar-more': 'More',
114 // Default label for the accept button of a confirmation dialog
115 'ooui-dialog-message-accept': 'OK',
116 // Default label for the reject button of a confirmation dialog
117 'ooui-dialog-message-reject': 'Cancel',
118 // Title for process dialog error description
119 'ooui-dialog-process-error': 'Something went wrong',
120 // Label for process dialog dismiss error button, visible when describing errors
121 'ooui-dialog-process-dismiss': 'Dismiss',
122 // Label for process dialog retry action button, visible when describing recoverable errors
123 'ooui-dialog-process-retry': 'Try again'
127 * Get a localized message.
129 * In environments that provide a localization system, this function should be overridden to
130 * return the message translated in the user's language. The default implementation always returns
133 * After the message key, message parameters may optionally be passed. In the default implementation,
134 * any occurrences of $1 are replaced with the first parameter, $2 with the second parameter, etc.
135 * Alternative implementations of OO.ui.msg may use any substitution system they like, as long as
136 * they support unnamed, ordered message parameters.
139 * @param {string} key Message key
140 * @param {Mixed...} [params] Message parameters
141 * @return {string} Translated message with parameters substituted
143 OO
.ui
.msg = function ( key
) {
144 var message
= messages
[key
], params
= Array
.prototype.slice
.call( arguments
, 1 );
145 if ( typeof message
=== 'string' ) {
146 // Perform $1 substitution
147 message
= message
.replace( /\$(\d+)/g, function ( unused
, n
) {
148 var i
= parseInt( n
, 10 );
149 return params
[i
- 1] !== undefined ? params
[i
- 1] : '$' + n
;
152 // Return placeholder if message not found
153 message
= '[' + key
+ ']';
159 * Package a message and arguments for deferred resolution.
161 * Use this when you are statically specifying a message and the message may not yet be present.
163 * @param {string} key Message key
164 * @param {Mixed...} [params] Message parameters
165 * @return {Function} Function that returns the resolved message when executed
167 OO
.ui
.deferMsg = function () {
168 var args
= arguments
;
170 return OO
.ui
.msg
.apply( OO
.ui
, args
);
177 * If the message is a function it will be executed, otherwise it will pass through directly.
179 * @param {Function|string} msg Deferred message, or message text
180 * @return {string} Resolved message
182 OO
.ui
.resolveMsg = function ( msg
) {
183 if ( $.isFunction( msg
) ) {
196 * @mixins OO.EventEmitter
199 * @param {Object} [config] Configuration options
201 OO
.ui
.ActionSet
= function OoUiActionSet( config
) {
202 // Configuration intialization
203 config
= config
|| {};
205 // Mixin constructors
206 OO
.EventEmitter
.call( this );
211 actions
: 'getAction',
215 this.categorized
= {};
218 this.organized
= false;
219 this.changing
= false;
220 this.changed
= false;
225 OO
.mixinClass( OO
.ui
.ActionSet
, OO
.EventEmitter
);
227 /* Static Properties */
230 * Symbolic name of dialog.
237 OO
.ui
.ActionSet
.static.specialFlags
= [ 'safe', 'primary' ];
243 * @param {OO.ui.ActionWidget} action Action that was clicked
248 * @param {OO.ui.ActionWidget} action Action that was resized
253 * @param {OO.ui.ActionWidget[]} added Actions added
258 * @param {OO.ui.ActionWidget[]} added Actions removed
268 * Handle action change events.
272 OO
.ui
.ActionSet
.prototype.onActionChange = function () {
273 this.organized
= false;
274 if ( this.changing
) {
277 this.emit( 'change' );
282 * Check if a action is one of the special actions.
284 * @param {OO.ui.ActionWidget} action Action to check
285 * @return {boolean} Action is special
287 OO
.ui
.ActionSet
.prototype.isSpecial = function ( action
) {
290 for ( flag
in this.special
) {
291 if ( action
=== this.special
[flag
] ) {
302 * @param {Object} [filters] Filters to use, omit to get all actions
303 * @param {string|string[]} [filters.actions] Actions that actions must have
304 * @param {string|string[]} [filters.flags] Flags that actions must have
305 * @param {string|string[]} [filters.modes] Modes that actions must have
306 * @param {boolean} [filters.visible] Actions must be visible
307 * @param {boolean} [filters.disabled] Actions must be disabled
308 * @return {OO.ui.ActionWidget[]} Actions matching all criteria
310 OO
.ui
.ActionSet
.prototype.get = function ( filters
) {
311 var i
, len
, list
, category
, actions
, index
, match
, matches
;
316 // Collect category candidates
318 for ( category
in this.categorized
) {
319 list
= filters
[category
];
321 if ( !Array
.isArray( list
) ) {
324 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
325 actions
= this.categorized
[category
][list
[i
]];
326 if ( Array
.isArray( actions
) ) {
327 matches
.push
.apply( matches
, actions
);
332 // Remove by boolean filters
333 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
336 ( filters
.visible
!== undefined && match
.isVisible() !== filters
.visible
) ||
337 ( filters
.disabled
!== undefined && match
.isDisabled() !== filters
.disabled
)
339 matches
.splice( i
, 1 );
345 for ( i
= 0, len
= matches
.length
; i
< len
; i
++ ) {
347 index
= matches
.lastIndexOf( match
);
348 while ( index
!== i
) {
349 matches
.splice( index
, 1 );
351 index
= matches
.lastIndexOf( match
);
356 return this.list
.slice();
360 * Get special actions.
362 * Special actions are the first visible actions with special flags, such as 'safe' and 'primary'.
363 * Special flags can be configured by changing #static-specialFlags in a subclass.
365 * @return {OO.ui.ActionWidget|null} Safe action
367 OO
.ui
.ActionSet
.prototype.getSpecial = function () {
369 return $.extend( {}, this.special
);
375 * Other actions include all non-special visible actions.
377 * @return {OO.ui.ActionWidget[]} Other actions
379 OO
.ui
.ActionSet
.prototype.getOthers = function () {
381 return this.others
.slice();
385 * Toggle actions based on their modes.
387 * Unlike calling toggle on actions with matching flags, this will enforce mutually exclusive
388 * visibility; matching actions will be shown, non-matching actions will be hidden.
390 * @param {string} mode Mode actions must have
395 OO
.ui
.ActionSet
.prototype.setMode = function ( mode
) {
398 this.changing
= true;
399 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
400 action
= this.list
[i
];
401 action
.toggle( action
.hasMode( mode
) );
404 this.organized
= false;
405 this.changing
= false;
406 this.emit( 'change' );
412 * Change which actions are able to be performed.
414 * Actions with matching actions will be disabled/enabled. Other actions will not be changed.
416 * @param {Object.<string,boolean>} actions List of abilities, keyed by action name, values
417 * indicate actions are able to be performed
420 OO
.ui
.ActionSet
.prototype.setAbilities = function ( actions
) {
421 var i
, len
, action
, item
;
423 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
425 action
= item
.getAction();
426 if ( actions
[action
] !== undefined ) {
427 item
.setDisabled( !actions
[action
] );
435 * Executes a function once per action.
437 * When making changes to multiple actions, use this method instead of iterating over the actions
438 * manually to defer emitting a change event until after all actions have been changed.
440 * @param {Object|null} actions Filters to use for which actions to iterate over; see #get
441 * @param {Function} callback Callback to run for each action; callback is invoked with three
442 * arguments: the action, the action's index, the list of actions being iterated over
445 OO
.ui
.ActionSet
.prototype.forEach = function ( filter
, callback
) {
446 this.changed
= false;
447 this.changing
= true;
448 this.get( filter
).forEach( callback
);
449 this.changing
= false;
450 if ( this.changed
) {
451 this.emit( 'change' );
460 * @param {OO.ui.ActionWidget[]} actions Actions to add
465 OO
.ui
.ActionSet
.prototype.add = function ( actions
) {
468 this.changing
= true;
469 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
471 action
.connect( this, {
472 click
: [ 'emit', 'click', action
],
473 resize
: [ 'emit', 'resize', action
],
474 toggle
: [ 'onActionChange' ]
476 this.list
.push( action
);
478 this.organized
= false;
479 this.emit( 'add', actions
);
480 this.changing
= false;
481 this.emit( 'change' );
489 * @param {OO.ui.ActionWidget[]} actions Actions to remove
494 OO
.ui
.ActionSet
.prototype.remove = function ( actions
) {
495 var i
, len
, index
, action
;
497 this.changing
= true;
498 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
500 index
= this.list
.indexOf( action
);
501 if ( index
!== -1 ) {
502 action
.disconnect( this );
503 this.list
.splice( index
, 1 );
506 this.organized
= false;
507 this.emit( 'remove', actions
);
508 this.changing
= false;
509 this.emit( 'change' );
515 * Remove all actions.
521 OO
.ui
.ActionSet
.prototype.clear = function () {
523 removed
= this.list
.slice();
525 this.changing
= true;
526 for ( i
= 0, len
= this.list
.length
; i
< len
; i
++ ) {
527 action
= this.list
[i
];
528 action
.disconnect( this );
533 this.organized
= false;
534 this.emit( 'remove', removed
);
535 this.changing
= false;
536 this.emit( 'change' );
544 * This is called whenver organized information is requested. It will only reorganize the actions
545 * if something has changed since the last time it ran.
550 OO
.ui
.ActionSet
.prototype.organize = function () {
551 var i
, iLen
, j
, jLen
, flag
, action
, category
, list
, item
, special
,
552 specialFlags
= this.constructor.static.specialFlags
;
554 if ( !this.organized
) {
555 this.categorized
= {};
558 for ( i
= 0, iLen
= this.list
.length
; i
< iLen
; i
++ ) {
559 action
= this.list
[i
];
560 if ( action
.isVisible() ) {
561 // Populate catgeories
562 for ( category
in this.categories
) {
563 if ( !this.categorized
[category
] ) {
564 this.categorized
[category
] = {};
566 list
= action
[this.categories
[category
]]();
567 if ( !Array
.isArray( list
) ) {
570 for ( j
= 0, jLen
= list
.length
; j
< jLen
; j
++ ) {
572 if ( !this.categorized
[category
][item
] ) {
573 this.categorized
[category
][item
] = [];
575 this.categorized
[category
][item
].push( action
);
578 // Populate special/others
580 for ( j
= 0, jLen
= specialFlags
.length
; j
< jLen
; j
++ ) {
581 flag
= specialFlags
[j
];
582 if ( !this.special
[flag
] && action
.hasFlag( flag
) ) {
583 this.special
[flag
] = action
;
589 this.others
.push( action
);
593 this.organized
= true;
600 * DOM element abstraction.
606 * @param {Object} [config] Configuration options
607 * @cfg {Function} [$] jQuery for the frame the widget is in
608 * @cfg {string[]} [classes] CSS class names
609 * @cfg {string} [text] Text to insert
610 * @cfg {jQuery} [$content] Content elements to append (after text)
612 OO
.ui
.Element
= function OoUiElement( config
) {
613 // Configuration initialization
614 config
= config
|| {};
617 this.$ = config
.$ || OO
.ui
.Element
.getJQuery( document
);
618 this.$element
= this.$( this.$.context
.createElement( this.getTagName() ) );
619 this.elementGroup
= null;
622 if ( $.isArray( config
.classes
) ) {
623 this.$element
.addClass( config
.classes
.join( ' ' ) );
626 this.$element
.text( config
.text
);
628 if ( config
.$content
) {
629 this.$element
.append( config
.$content
);
635 OO
.initClass( OO
.ui
.Element
);
637 /* Static Properties */
642 * This may be ignored if getTagName is overridden.
648 OO
.ui
.Element
.static.tagName
= 'div';
653 * Get a jQuery function within a specific document.
656 * @param {jQuery|HTMLElement|HTMLDocument|Window} context Context to bind the function to
657 * @param {jQuery} [$iframe] HTML iframe element that contains the document, omit if document is
659 * @return {Function} Bound jQuery function
661 OO
.ui
.Element
.getJQuery = function ( context
, $iframe
) {
662 function wrapper( selector
) {
663 return $( selector
, wrapper
.context
);
666 wrapper
.context
= this.getDocument( context
);
669 wrapper
.$iframe
= $iframe
;
676 * Get the document of an element.
679 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Object to get the document for
680 * @return {HTMLDocument|null} Document object
682 OO
.ui
.Element
.getDocument = function ( obj
) {
683 // jQuery - selections created "offscreen" won't have a context, so .context isn't reliable
684 return ( obj
[0] && obj
[0].ownerDocument
) ||
685 // Empty jQuery selections might have a context
692 ( obj
.nodeType
=== 9 && obj
) ||
697 * Get the window of an element or document.
700 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the window for
701 * @return {Window} Window object
703 OO
.ui
.Element
.getWindow = function ( obj
) {
704 var doc
= this.getDocument( obj
);
705 return doc
.parentWindow
|| doc
.defaultView
;
709 * Get the direction of an element or document.
712 * @param {jQuery|HTMLElement|HTMLDocument|Window} obj Context to get the direction for
713 * @return {string} Text direction, either `ltr` or `rtl`
715 OO
.ui
.Element
.getDir = function ( obj
) {
718 if ( obj
instanceof jQuery
) {
721 isDoc
= obj
.nodeType
=== 9;
722 isWin
= obj
.document
!== undefined;
723 if ( isDoc
|| isWin
) {
729 return $( obj
).css( 'direction' );
733 * Get the offset between two frames.
735 * TODO: Make this function not use recursion.
738 * @param {Window} from Window of the child frame
739 * @param {Window} [to=window] Window of the parent frame
740 * @param {Object} [offset] Offset to start with, used internally
741 * @return {Object} Offset object, containing left and top properties
743 OO
.ui
.Element
.getFrameOffset = function ( from, to
, offset
) {
744 var i
, len
, frames
, frame
, rect
;
750 offset
= { top
: 0, left
: 0 };
752 if ( from.parent
=== from ) {
756 // Get iframe element
757 frames
= from.parent
.document
.getElementsByTagName( 'iframe' );
758 for ( i
= 0, len
= frames
.length
; i
< len
; i
++ ) {
759 if ( frames
[i
].contentWindow
=== from ) {
765 // Recursively accumulate offset values
767 rect
= frame
.getBoundingClientRect();
768 offset
.left
+= rect
.left
;
769 offset
.top
+= rect
.top
;
771 this.getFrameOffset( from.parent
, offset
);
778 * Get the offset between two elements.
781 * @param {jQuery} $from
782 * @param {jQuery} $to
783 * @return {Object} Translated position coordinates, containing top and left properties
785 OO
.ui
.Element
.getRelativePosition = function ( $from, $to
) {
786 var from = $from.offset(),
788 return { top
: Math
.round( from.top
- to
.top
), left
: Math
.round( from.left
- to
.left
) };
792 * Get element border sizes.
795 * @param {HTMLElement} el Element to measure
796 * @return {Object} Dimensions object with `top`, `left`, `bottom` and `right` properties
798 OO
.ui
.Element
.getBorders = function ( el
) {
799 var doc
= el
.ownerDocument
,
800 win
= doc
.parentWindow
|| doc
.defaultView
,
801 style
= win
&& win
.getComputedStyle
?
802 win
.getComputedStyle( el
, null ) :
805 top
= parseFloat( style
? style
.borderTopWidth
: $el
.css( 'borderTopWidth' ) ) || 0,
806 left
= parseFloat( style
? style
.borderLeftWidth
: $el
.css( 'borderLeftWidth' ) ) || 0,
807 bottom
= parseFloat( style
? style
.borderBottomWidth
: $el
.css( 'borderBottomWidth' ) ) || 0,
808 right
= parseFloat( style
? style
.borderRightWidth
: $el
.css( 'borderRightWidth' ) ) || 0;
811 top
: Math
.round( top
),
812 left
: Math
.round( left
),
813 bottom
: Math
.round( bottom
),
814 right
: Math
.round( right
)
819 * Get dimensions of an element or window.
822 * @param {HTMLElement|Window} el Element to measure
823 * @return {Object} Dimensions object with `borders`, `scroll`, `scrollbar` and `rect` properties
825 OO
.ui
.Element
.getDimensions = function ( el
) {
827 doc
= el
.ownerDocument
|| el
.document
,
828 win
= doc
.parentWindow
|| doc
.defaultView
;
830 if ( win
=== el
|| el
=== doc
.documentElement
) {
833 borders
: { top
: 0, left
: 0, bottom
: 0, right
: 0 },
835 top
: $win
.scrollTop(),
836 left
: $win
.scrollLeft()
838 scrollbar
: { right
: 0, bottom
: 0 },
842 bottom
: $win
.innerHeight(),
843 right
: $win
.innerWidth()
849 borders
: this.getBorders( el
),
851 top
: $el
.scrollTop(),
852 left
: $el
.scrollLeft()
855 right
: $el
.innerWidth() - el
.clientWidth
,
856 bottom
: $el
.innerHeight() - el
.clientHeight
858 rect
: el
.getBoundingClientRect()
864 * Get closest scrollable container.
866 * Traverses up until either a scrollable element or the root is reached, in which case the window
870 * @param {HTMLElement} el Element to find scrollable container for
871 * @param {string} [dimension] Dimension of scrolling to look for; `x`, `y` or omit for either
872 * @return {HTMLElement|Window} Closest scrollable container
874 OO
.ui
.Element
.getClosestScrollableContainer = function ( el
, dimension
) {
876 props
= [ 'overflow' ],
877 $parent
= $( el
).parent();
879 if ( dimension
=== 'x' || dimension
=== 'y' ) {
880 props
.push( 'overflow-' + dimension
);
883 while ( $parent
.length
) {
884 if ( $parent
[0] === el
.ownerDocument
.body
) {
889 val
= $parent
.css( props
[i
] );
890 if ( val
=== 'auto' || val
=== 'scroll' ) {
894 $parent
= $parent
.parent();
896 return this.getDocument( el
).body
;
900 * Scroll element into view.
903 * @param {HTMLElement} el Element to scroll into view
904 * @param {Object} [config={}] Configuration config
905 * @param {string} [config.duration] jQuery animation duration value
906 * @param {string} [config.direction] Scroll in only one direction, e.g. 'x' or 'y', omit
907 * to scroll in both directions
908 * @param {Function} [config.complete] Function to call when scrolling completes
910 OO
.ui
.Element
.scrollIntoView = function ( el
, config
) {
911 // Configuration initialization
912 config
= config
|| {};
915 callback
= typeof config
.complete
=== 'function' && config
.complete
,
916 sc
= this.getClosestScrollableContainer( el
, config
.direction
),
918 eld
= this.getDimensions( el
),
919 scd
= this.getDimensions( sc
),
920 $win
= $( this.getWindow( el
) );
922 // Compute the distances between the edges of el and the edges of the scroll viewport
923 if ( $sc
.is( 'body' ) ) {
924 // If the scrollable container is the <body> this is easy
927 bottom
: $win
.innerHeight() - eld
.rect
.bottom
,
929 right
: $win
.innerWidth() - eld
.rect
.right
932 // Otherwise, we have to subtract el's coordinates from sc's coordinates
934 top
: eld
.rect
.top
- ( scd
.rect
.top
+ scd
.borders
.top
),
935 bottom
: scd
.rect
.bottom
- scd
.borders
.bottom
- scd
.scrollbar
.bottom
- eld
.rect
.bottom
,
936 left
: eld
.rect
.left
- ( scd
.rect
.left
+ scd
.borders
.left
),
937 right
: scd
.rect
.right
- scd
.borders
.right
- scd
.scrollbar
.right
- eld
.rect
.right
941 if ( !config
.direction
|| config
.direction
=== 'y' ) {
943 anim
.scrollTop
= scd
.scroll
.top
+ rel
.top
;
944 } else if ( rel
.top
> 0 && rel
.bottom
< 0 ) {
945 anim
.scrollTop
= scd
.scroll
.top
+ Math
.min( rel
.top
, -rel
.bottom
);
948 if ( !config
.direction
|| config
.direction
=== 'x' ) {
949 if ( rel
.left
< 0 ) {
950 anim
.scrollLeft
= scd
.scroll
.left
+ rel
.left
;
951 } else if ( rel
.left
> 0 && rel
.right
< 0 ) {
952 anim
.scrollLeft
= scd
.scroll
.left
+ Math
.min( rel
.left
, -rel
.right
);
955 if ( !$.isEmptyObject( anim
) ) {
956 $sc
.stop( true ).animate( anim
, config
.duration
|| 'fast' );
958 $sc
.queue( function ( next
) {
973 * Get the HTML tag name.
975 * Override this method to base the result on instance information.
977 * @return {string} HTML tag name
979 OO
.ui
.Element
.prototype.getTagName = function () {
980 return this.constructor.static.tagName
;
984 * Check if the element is attached to the DOM
985 * @return {boolean} The element is attached to the DOM
987 OO
.ui
.Element
.prototype.isElementAttached = function () {
988 return $.contains( this.getElementDocument(), this.$element
[0] );
992 * Get the DOM document.
994 * @return {HTMLDocument} Document object
996 OO
.ui
.Element
.prototype.getElementDocument = function () {
997 return OO
.ui
.Element
.getDocument( this.$element
);
1001 * Get the DOM window.
1003 * @return {Window} Window object
1005 OO
.ui
.Element
.prototype.getElementWindow = function () {
1006 return OO
.ui
.Element
.getWindow( this.$element
);
1010 * Get closest scrollable container.
1012 OO
.ui
.Element
.prototype.getClosestScrollableElementContainer = function () {
1013 return OO
.ui
.Element
.getClosestScrollableContainer( this.$element
[0] );
1017 * Get group element is in.
1019 * @return {OO.ui.GroupElement|null} Group element, null if none
1021 OO
.ui
.Element
.prototype.getElementGroup = function () {
1022 return this.elementGroup
;
1026 * Set group element is in.
1028 * @param {OO.ui.GroupElement|null} group Group element, null if none
1031 OO
.ui
.Element
.prototype.setElementGroup = function ( group
) {
1032 this.elementGroup
= group
;
1037 * Scroll element into view.
1039 * @param {Object} [config={}]
1041 OO
.ui
.Element
.prototype.scrollElementIntoView = function ( config
) {
1042 return OO
.ui
.Element
.scrollIntoView( this.$element
[0], config
);
1046 * Bind a handler for an event on this.$element
1048 * @deprecated Use jQuery#on instead.
1049 * @param {string} event
1050 * @param {Function} callback
1052 OO
.ui
.Element
.prototype.onDOMEvent = function ( event
, callback
) {
1053 OO
.ui
.Element
.onDOMEvent( this.$element
, event
, callback
);
1057 * Unbind a handler bound with #offDOMEvent
1059 * @deprecated Use jQuery#off instead.
1060 * @param {string} event
1061 * @param {Function} callback
1063 OO
.ui
.Element
.prototype.offDOMEvent = function ( event
, callback
) {
1064 OO
.ui
.Element
.offDOMEvent( this.$element
, event
, callback
);
1069 * Bind a handler for an event on a DOM element.
1071 * Used to be for working around a jQuery bug (jqbug.com/14180),
1072 * but obsolete as of jQuery 1.11.0.
1075 * @deprecated Use jQuery#on instead.
1076 * @param {HTMLElement|jQuery} el DOM element
1077 * @param {string} event Event to bind
1078 * @param {Function} callback Callback to call when the event fires
1080 OO
.ui
.Element
.onDOMEvent = function ( el
, event
, callback
) {
1081 $( el
).on( event
, callback
);
1085 * Unbind a handler bound with #static-method-onDOMEvent.
1087 * @deprecated Use jQuery#off instead.
1089 * @param {HTMLElement|jQuery} el DOM element
1090 * @param {string} event Event to unbind
1091 * @param {Function} [callback] Callback to unbind
1093 OO
.ui
.Element
.offDOMEvent = function ( el
, event
, callback
) {
1094 $( el
).off( event
, callback
);
1099 * Container for elements.
1103 * @extends OO.ui.Element
1104 * @mixins OO.EventEmitter
1107 * @param {Object} [config] Configuration options
1109 OO
.ui
.Layout
= function OoUiLayout( config
) {
1110 // Initialize config
1111 config
= config
|| {};
1113 // Parent constructor
1114 OO
.ui
.Layout
.super.call( this, config
);
1116 // Mixin constructors
1117 OO
.EventEmitter
.call( this );
1120 this.$element
.addClass( 'oo-ui-layout' );
1125 OO
.inheritClass( OO
.ui
.Layout
, OO
.ui
.Element
);
1126 OO
.mixinClass( OO
.ui
.Layout
, OO
.EventEmitter
);
1129 * User interface control.
1133 * @extends OO.ui.Element
1134 * @mixins OO.EventEmitter
1137 * @param {Object} [config] Configuration options
1138 * @cfg {boolean} [disabled=false] Disable
1140 OO
.ui
.Widget
= function OoUiWidget( config
) {
1141 // Initialize config
1142 config
= $.extend( { disabled
: false }, config
);
1144 // Parent constructor
1145 OO
.ui
.Widget
.super.call( this, config
);
1147 // Mixin constructors
1148 OO
.EventEmitter
.call( this );
1151 this.visible
= true;
1152 this.disabled
= null;
1153 this.wasDisabled
= null;
1156 this.$element
.addClass( 'oo-ui-widget' );
1157 this.setDisabled( !!config
.disabled
);
1162 OO
.inheritClass( OO
.ui
.Widget
, OO
.ui
.Element
);
1163 OO
.mixinClass( OO
.ui
.Widget
, OO
.EventEmitter
);
1169 * @param {boolean} disabled Widget is disabled
1174 * @param {boolean} visible Widget is visible
1180 * Check if the widget is disabled.
1182 * @param {boolean} Button is disabled
1184 OO
.ui
.Widget
.prototype.isDisabled = function () {
1185 return this.disabled
;
1189 * Check if widget is visible.
1191 * @return {boolean} Widget is visible
1193 OO
.ui
.Widget
.prototype.isVisible = function () {
1194 return this.visible
;
1198 * Set the disabled state of the widget.
1200 * This should probably change the widgets' appearance and prevent it from being used.
1202 * @param {boolean} disabled Disable widget
1205 OO
.ui
.Widget
.prototype.setDisabled = function ( disabled
) {
1208 this.disabled
= !!disabled
;
1209 isDisabled
= this.isDisabled();
1210 if ( isDisabled
!== this.wasDisabled
) {
1211 this.$element
.toggleClass( 'oo-ui-widget-disabled', isDisabled
);
1212 this.$element
.toggleClass( 'oo-ui-widget-enabled', !isDisabled
);
1213 this.emit( 'disable', isDisabled
);
1215 this.wasDisabled
= isDisabled
;
1221 * Toggle visibility of widget.
1223 * @param {boolean} [show] Make widget visible, omit to toggle visibility
1227 OO
.ui
.Widget
.prototype.toggle = function ( show
) {
1228 show
= show
=== undefined ? !this.visible
: !!show
;
1230 if ( show
!== this.isVisible() ) {
1231 this.visible
= show
;
1232 this.$element
.toggle( show
);
1233 this.emit( 'toggle', show
);
1240 * Update the disabled state, in case of changes in parent widget.
1244 OO
.ui
.Widget
.prototype.updateDisabled = function () {
1245 this.setDisabled( this.disabled
);
1250 * Container for elements in a child frame.
1252 * Use together with OO.ui.WindowManager.
1256 * @extends OO.ui.Element
1257 * @mixins OO.EventEmitter
1259 * When a window is opened, the setup and ready processes are executed. Similarly, the hold and
1260 * teardown processes are executed when the window is closed.
1262 * - {@link OO.ui.WindowManager#openWindow} or {@link #open} methods are used to start opening
1263 * - Window manager begins opening window
1264 * - {@link #getSetupProcess} method is called and its result executed
1265 * - {@link #getReadyProcess} method is called and its result executed
1266 * - Window is now open
1268 * - {@link OO.ui.WindowManager#closeWindow} or {@link #close} methods are used to start closing
1269 * - Window manager begins closing window
1270 * - {@link #getHoldProcess} method is called and its result executed
1271 * - {@link #getTeardownProcess} method is called and its result executed
1272 * - Window is now closed
1274 * Each process (setup, ready, hold and teardown) can be extended in subclasses by overriding
1275 * {@link #getSetupProcess}, {@link #getReadyProcess}, {@link #getHoldProcess} and
1276 * {@link #getTeardownProcess} respectively. Each process is executed in series, so asynchonous
1277 * processing can complete. Always assume window processes are executed asychronously. See
1278 * OO.ui.Process for more details about how to work with processes. Some events, as well as the
1279 * #open and #close methods, provide promises which are resolved when the window enters a new state.
1281 * Sizing of windows is specified using symbolic names which are interpreted by the window manager.
1282 * If the requested size is not recognized, the window manager will choose a sensible fallback.
1285 * @param {Object} [config] Configuration options
1286 * @cfg {string} [size] Symbolic name of dialog size, `small`, `medium`, `large` or `full`; omit to
1290 OO
.ui
.Window
= function OoUiWindow( config
) {
1291 // Configuration initialization
1292 config
= config
|| {};
1294 // Parent constructor
1295 OO
.ui
.Window
.super.call( this, config
);
1297 // Mixin constructors
1298 OO
.EventEmitter
.call( this );
1301 this.manager
= null;
1302 this.initialized
= false;
1303 this.visible
= false;
1304 this.opening
= null;
1305 this.closing
= null;
1308 this.loading
= null;
1309 this.size
= config
.size
|| this.constructor.static.size
;
1310 this.$frame
= this.$( '<div>' );
1314 .addClass( 'oo-ui-window' )
1315 .append( this.$frame
);
1316 this.$frame
.addClass( 'oo-ui-window-frame' );
1318 // NOTE: Additional intitialization will occur when #setManager is called
1323 OO
.inheritClass( OO
.ui
.Window
, OO
.ui
.Element
);
1324 OO
.mixinClass( OO
.ui
.Window
, OO
.EventEmitter
);
1330 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1333 /* Static Properties */
1336 * Symbolic name of size.
1338 * Size is used if no size is configured during construction.
1342 * @property {string}
1344 OO
.ui
.Window
.static.size
= 'medium';
1346 /* Static Methods */
1349 * Transplant the CSS styles from as parent document to a frame's document.
1351 * This loops over the style sheets in the parent document, and copies their nodes to the
1352 * frame's document. It then polls the document to see when all styles have loaded, and once they
1353 * have, resolves the promise.
1355 * If the styles still haven't loaded after a long time (5 seconds by default), we give up waiting
1356 * and resolve the promise anyway. This protects against cases like a display: none; iframe in
1357 * Firefox, where the styles won't load until the iframe becomes visible.
1359 * For details of how we arrived at the strategy used in this function, see #load.
1363 * @param {HTMLDocument} parentDoc Document to transplant styles from
1364 * @param {HTMLDocument} frameDoc Document to transplant styles to
1365 * @param {number} [timeout=5000] How long to wait before giving up (in ms). If 0, never give up.
1366 * @return {jQuery.Promise} Promise resolved when styles have loaded
1368 OO
.ui
.Window
.static.transplantStyles = function ( parentDoc
, frameDoc
, timeout
) {
1369 var i
, numSheets
, styleNode
, styleText
, newNode
, timeoutID
, pollNodeId
, $pendingPollNodes
,
1370 $pollNodes
= $( [] ),
1371 // Fake font-family value
1372 fontFamily
= 'oo-ui-frame-transplantStyles-loaded',
1373 nextIndex
= parentDoc
.oouiFrameTransplantStylesNextIndex
|| 0,
1374 deferred
= $.Deferred();
1376 for ( i
= 0, numSheets
= parentDoc
.styleSheets
.length
; i
< numSheets
; i
++ ) {
1377 styleNode
= parentDoc
.styleSheets
[i
].ownerNode
;
1378 if ( styleNode
.disabled
) {
1382 if ( styleNode
.nodeName
.toLowerCase() === 'link' ) {
1383 // External stylesheet; use @import
1384 styleText
= '@import url(' + styleNode
.href
+ ');';
1386 // Internal stylesheet; just copy the text
1387 // For IE10 we need to fall back to .cssText, BUT that's undefined in
1388 // other browsers, so fall back to '' rather than 'undefined'
1389 styleText
= styleNode
.textContent
|| parentDoc
.styleSheets
[i
].cssText
|| '';
1392 // Create a node with a unique ID that we're going to monitor to see when the CSS
1394 if ( styleNode
.oouiFrameTransplantStylesId
) {
1395 // If we're nesting transplantStyles operations and this node already has
1396 // a CSS rule to wait for loading, reuse it
1397 pollNodeId
= styleNode
.oouiFrameTransplantStylesId
;
1399 // Otherwise, create a new ID
1400 pollNodeId
= 'oo-ui-frame-transplantStyles-loaded-' + nextIndex
;
1403 // Add #pollNodeId { font-family: ... } to the end of the stylesheet / after the @import
1404 // The font-family rule will only take effect once the @import finishes
1405 styleText
+= '\n' + '#' + pollNodeId
+ ' { font-family: ' + fontFamily
+ '; }';
1408 // Create a node with id=pollNodeId
1409 $pollNodes
= $pollNodes
.add( $( '<div>', frameDoc
)
1410 .attr( 'id', pollNodeId
)
1411 .appendTo( frameDoc
.body
)
1414 // Add our modified CSS as a <style> tag
1415 newNode
= frameDoc
.createElement( 'style' );
1416 newNode
.textContent
= styleText
;
1417 newNode
.oouiFrameTransplantStylesId
= pollNodeId
;
1418 frameDoc
.head
.appendChild( newNode
);
1420 frameDoc
.oouiFrameTransplantStylesNextIndex
= nextIndex
;
1422 // Poll every 100ms until all external stylesheets have loaded
1423 $pendingPollNodes
= $pollNodes
;
1424 timeoutID
= setTimeout( function pollExternalStylesheets() {
1426 $pendingPollNodes
.length
> 0 &&
1427 $pendingPollNodes
.eq( 0 ).css( 'font-family' ) === fontFamily
1429 $pendingPollNodes
= $pendingPollNodes
.slice( 1 );
1432 if ( $pendingPollNodes
.length
=== 0 ) {
1434 if ( timeoutID
!== null ) {
1436 $pollNodes
.remove();
1440 timeoutID
= setTimeout( pollExternalStylesheets
, 100 );
1443 // ...but give up after a while
1444 if ( timeout
!== 0 ) {
1445 setTimeout( function () {
1447 clearTimeout( timeoutID
);
1449 $pollNodes
.remove();
1452 }, timeout
|| 5000 );
1455 return deferred
.promise();
1461 * Handle mouse down events.
1463 * @param {jQuery.Event} e Mouse down event
1465 OO
.ui
.Window
.prototype.onMouseDown = function ( e
) {
1466 // Prevent clicking on the click-block from stealing focus
1467 if ( e
.target
=== this.$element
[0] ) {
1473 * Check if window has been initialized.
1475 * @return {boolean} Window has been initialized
1477 OO
.ui
.Window
.prototype.isInitialized = function () {
1478 return this.initialized
;
1482 * Check if window is visible.
1484 * @return {boolean} Window is visible
1486 OO
.ui
.Window
.prototype.isVisible = function () {
1487 return this.visible
;
1491 * Check if window is loading.
1493 * @return {boolean} Window is loading
1495 OO
.ui
.Window
.prototype.isLoading = function () {
1496 return this.loading
&& this.loading
.state() === 'pending';
1500 * Check if window is loaded.
1502 * @return {boolean} Window is loaded
1504 OO
.ui
.Window
.prototype.isLoaded = function () {
1505 return this.loading
&& this.loading
.state() === 'resolved';
1509 * Check if window is opening.
1511 * This is a wrapper around OO.ui.WindowManager#isOpening.
1513 * @return {boolean} Window is opening
1515 OO
.ui
.Window
.prototype.isOpening = function () {
1516 return this.manager
.isOpening( this );
1520 * Check if window is closing.
1522 * This is a wrapper around OO.ui.WindowManager#isClosing.
1524 * @return {boolean} Window is closing
1526 OO
.ui
.Window
.prototype.isClosing = function () {
1527 return this.manager
.isClosing( this );
1531 * Check if window is opened.
1533 * This is a wrapper around OO.ui.WindowManager#isOpened.
1535 * @return {boolean} Window is opened
1537 OO
.ui
.Window
.prototype.isOpened = function () {
1538 return this.manager
.isOpened( this );
1542 * Get the window manager.
1544 * @return {OO.ui.WindowManager} Manager of window
1546 OO
.ui
.Window
.prototype.getManager = function () {
1547 return this.manager
;
1551 * Get the window size.
1553 * @return {string} Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1555 OO
.ui
.Window
.prototype.getSize = function () {
1560 * Get the height of the dialog contents.
1562 * @return {number} Content height
1564 OO
.ui
.Window
.prototype.getContentHeight = function () {
1566 // Add buffer for border
1567 ( this.$frame
.outerHeight() - this.$frame
.innerHeight() ) +
1568 // Use combined heights of children
1569 ( this.$head
.outerHeight( true ) + this.getBodyHeight() + this.$foot
.outerHeight( true ) )
1574 * Get the height of the dialog contents.
1576 * @return {number} Height of content
1578 OO
.ui
.Window
.prototype.getBodyHeight = function () {
1579 return this.$body
[0].scrollHeight
;
1583 * Get the directionality of the frame
1585 * @return {string} Directionality, 'ltr' or 'rtl'
1587 OO
.ui
.Window
.prototype.getDir = function () {
1592 * Get a process for setting up a window for use.
1594 * Each time the window is opened this process will set it up for use in a particular context, based
1595 * on the `data` argument.
1597 * When you override this method, you can add additional setup steps to the process the parent
1598 * method provides using the 'first' and 'next' methods.
1601 * @param {Object} [data] Window opening data
1602 * @return {OO.ui.Process} Setup process
1604 OO
.ui
.Window
.prototype.getSetupProcess = function () {
1605 return new OO
.ui
.Process();
1609 * Get a process for readying a window for use.
1611 * Each time the window is open and setup, this process will ready it up for use in a particular
1612 * context, based on the `data` argument.
1614 * When you override this method, you can add additional setup steps to the process the parent
1615 * method provides using the 'first' and 'next' methods.
1618 * @param {Object} [data] Window opening data
1619 * @return {OO.ui.Process} Setup process
1621 OO
.ui
.Window
.prototype.getReadyProcess = function () {
1622 return new OO
.ui
.Process();
1626 * Get a process for holding a window from use.
1628 * Each time the window is closed, this process will hold it from use in a particular context, based
1629 * on the `data` argument.
1631 * When you override this method, you can add additional setup steps to the process the parent
1632 * method provides using the 'first' and 'next' methods.
1635 * @param {Object} [data] Window closing data
1636 * @return {OO.ui.Process} Hold process
1638 OO
.ui
.Window
.prototype.getHoldProcess = function () {
1639 return new OO
.ui
.Process();
1643 * Get a process for tearing down a window after use.
1645 * Each time the window is closed this process will tear it down and do something with the user's
1646 * interactions within the window, based on the `data` argument.
1648 * When you override this method, you can add additional teardown steps to the process the parent
1649 * method provides using the 'first' and 'next' methods.
1652 * @param {Object} [data] Window closing data
1653 * @return {OO.ui.Process} Teardown process
1655 OO
.ui
.Window
.prototype.getTeardownProcess = function () {
1656 return new OO
.ui
.Process();
1660 * Toggle visibility of window.
1662 * If the window is isolated and hasn't fully loaded yet, the visiblity property will be used
1663 * instead of display.
1665 * @param {boolean} [show] Make window visible, omit to toggle visibility
1669 OO
.ui
.Window
.prototype.toggle = function ( show
) {
1670 show
= show
=== undefined ? !this.visible
: !!show
;
1672 if ( show
!== this.isVisible() ) {
1673 this.visible
= show
;
1675 if ( this.isolated
&& !this.isLoaded() ) {
1676 // Hide the window using visibility instead of display until loading is complete
1677 // Can't use display: none; because that prevents the iframe from loading in Firefox
1678 this.$element
.css( 'visibility', show
? 'visible' : 'hidden' );
1680 this.$element
.toggle( show
).css( 'visibility', '' );
1682 this.emit( 'toggle', show
);
1689 * Set the window manager.
1691 * This must be called before initialize. Calling it more than once will cause an error.
1693 * @param {OO.ui.WindowManager} manager Manager for this window
1694 * @throws {Error} If called more than once
1697 OO
.ui
.Window
.prototype.setManager = function ( manager
) {
1698 if ( this.manager
) {
1699 throw new Error( 'Cannot set window manager, window already has a manager' );
1703 this.manager
= manager
;
1704 this.isolated
= manager
.shouldIsolate();
1707 if ( this.isolated
) {
1708 this.$iframe
= this.$( '<iframe>' );
1709 this.$iframe
.attr( { frameborder
: 0, scrolling
: 'no' } );
1710 this.$frame
.append( this.$iframe
);
1711 this.$ = function () {
1712 throw new Error( 'this.$() cannot be used until the frame has been initialized.' );
1714 // WARNING: Do not use this.$ again until #initialize is called
1716 this.$content
= this.$( '<div>' );
1717 this.$document
= $( this.getElementDocument() );
1718 this.$content
.addClass( 'oo-ui-window-content' );
1719 this.$frame
.append( this.$content
);
1721 this.toggle( false );
1723 // Figure out directionality:
1724 this.dir
= OO
.ui
.Element
.getDir( this.$iframe
|| this.$content
) || 'ltr';
1730 * Set the window size.
1732 * @param {string} size Symbolic size name, e.g. 'small', 'medium', 'large', 'full'
1735 OO
.ui
.Window
.prototype.setSize = function ( size
) {
1737 this.manager
.updateWindowSize( this );
1742 * Set window dimensions.
1744 * Properties are applied to the frame container.
1746 * @param {Object} dim CSS dimension properties
1747 * @param {string|number} [dim.width] Width
1748 * @param {string|number} [dim.minWidth] Minimum width
1749 * @param {string|number} [dim.maxWidth] Maximum width
1750 * @param {string|number} [dim.width] Height, omit to set based on height of contents
1751 * @param {string|number} [dim.minWidth] Minimum height
1752 * @param {string|number} [dim.maxWidth] Maximum height
1755 OO
.ui
.Window
.prototype.setDimensions = function ( dim
) {
1756 // Apply width before height so height is not based on wrapping content using the wrong width
1758 width
: dim
.width
|| '',
1759 minWidth
: dim
.minWidth
|| '',
1760 maxWidth
: dim
.maxWidth
|| ''
1763 height
: ( dim
.height
!== undefined ? dim
.height
: this.getContentHeight() ) || '',
1764 minHeight
: dim
.minHeight
|| '',
1765 maxHeight
: dim
.maxHeight
|| ''
1771 * Initialize window contents.
1773 * The first time the window is opened, #initialize is called when it's safe to begin populating
1774 * its contents. See #getSetupProcess for a way to make changes each time the window opens.
1776 * Once this method is called, this.$ can be used to create elements within the frame.
1778 * @throws {Error} If not attached to a manager
1781 OO
.ui
.Window
.prototype.initialize = function () {
1782 if ( !this.manager
) {
1783 throw new Error( 'Cannot initialize window, must be attached to a manager' );
1787 this.$head
= this.$( '<div>' );
1788 this.$body
= this.$( '<div>' );
1789 this.$foot
= this.$( '<div>' );
1790 this.$overlay
= this.$( '<div>' );
1793 this.$element
.on( 'mousedown', OO
.ui
.bind( this.onMouseDown
, this ) );
1796 this.$head
.addClass( 'oo-ui-window-head' );
1797 this.$body
.addClass( 'oo-ui-window-body' );
1798 this.$foot
.addClass( 'oo-ui-window-foot' );
1799 this.$overlay
.addClass( 'oo-ui-window-overlay' );
1800 this.$content
.append( this.$head
, this.$body
, this.$foot
, this.$overlay
);
1808 * This is a wrapper around calling {@link OO.ui.WindowManager#openWindow} on the window manager.
1809 * To do something each time the window opens, use #getSetupProcess or #getReadyProcess.
1811 * @param {Object} [data] Window opening data
1812 * @return {jQuery.Promise} Promise resolved when window is opened; when the promise is resolved the
1813 * first argument will be a promise which will be resolved when the window begins closing
1815 OO
.ui
.Window
.prototype.open = function ( data
) {
1816 return this.manager
.openWindow( this, data
);
1822 * This is a wrapper around calling OO.ui.WindowManager#closeWindow on the window manager.
1823 * To do something each time the window closes, use #getHoldProcess or #getTeardownProcess.
1825 * @param {Object} [data] Window closing data
1826 * @return {jQuery.Promise} Promise resolved when window is closed
1828 OO
.ui
.Window
.prototype.close = function ( data
) {
1829 return this.manager
.closeWindow( this, data
);
1835 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1838 * @param {Object} [data] Window opening data
1839 * @return {jQuery.Promise} Promise resolved when window is setup
1841 OO
.ui
.Window
.prototype.setup = function ( data
) {
1843 deferred
= $.Deferred();
1845 this.$element
.show();
1846 this.visible
= true;
1847 this.getSetupProcess( data
).execute().done( function () {
1848 // Force redraw by asking the browser to measure the elements' widths
1849 win
.$element
.addClass( 'oo-ui-window-setup' ).width();
1850 win
.$content
.addClass( 'oo-ui-window-content-setup' ).width();
1854 return deferred
.promise();
1860 * This is called by OO.ui.WindowManager durring window opening, and should not be called directly
1863 * @param {Object} [data] Window opening data
1864 * @return {jQuery.Promise} Promise resolved when window is ready
1866 OO
.ui
.Window
.prototype.ready = function ( data
) {
1868 deferred
= $.Deferred();
1870 this.$content
.focus();
1871 this.getReadyProcess( data
).execute().done( function () {
1872 // Force redraw by asking the browser to measure the elements' widths
1873 win
.$element
.addClass( 'oo-ui-window-ready' ).width();
1874 win
.$content
.addClass( 'oo-ui-window-content-ready' ).width();
1878 return deferred
.promise();
1884 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1887 * @param {Object} [data] Window closing data
1888 * @return {jQuery.Promise} Promise resolved when window is held
1890 OO
.ui
.Window
.prototype.hold = function ( data
) {
1892 deferred
= $.Deferred();
1894 this.getHoldProcess( data
).execute().done( function () {
1895 // Get the focused element within the window's content
1896 var $focus
= win
.$content
.find( OO
.ui
.Element
.getDocument( win
.$content
).activeElement
);
1898 // Blur the focused element
1899 if ( $focus
.length
) {
1903 // Force redraw by asking the browser to measure the elements' widths
1904 win
.$element
.removeClass( 'oo-ui-window-ready' ).width();
1905 win
.$content
.removeClass( 'oo-ui-window-content-ready' ).width();
1909 return deferred
.promise();
1915 * This is called by OO.ui.WindowManager durring window closing, and should not be called directly
1918 * @param {Object} [data] Window closing data
1919 * @return {jQuery.Promise} Promise resolved when window is torn down
1921 OO
.ui
.Window
.prototype.teardown = function ( data
) {
1923 deferred
= $.Deferred();
1925 this.getTeardownProcess( data
).execute().done( function () {
1926 // Force redraw by asking the browser to measure the elements' widths
1927 win
.$element
.removeClass( 'oo-ui-window-setup' ).width();
1928 win
.$content
.removeClass( 'oo-ui-window-content-setup' ).width();
1929 win
.$element
.hide();
1930 win
.visible
= false;
1934 return deferred
.promise();
1938 * Load the frame contents.
1940 * Once the iframe's stylesheets are loaded, the `load` event will be emitted and the returned
1941 * promise will be resolved. Calling while loading will return a promise but not trigger a new
1942 * loading cycle. Calling after loading is complete will return a promise that's already been
1945 * Sounds simple right? Read on...
1947 * When you create a dynamic iframe using open/write/close, the window.load event for the
1948 * iframe is triggered when you call close, and there's no further load event to indicate that
1949 * everything is actually loaded.
1951 * In Chrome, stylesheets don't show up in document.styleSheets until they have loaded, so we could
1952 * just poll that array and wait for it to have the right length. However, in Firefox, stylesheets
1953 * are added to document.styleSheets immediately, and the only way you can determine whether they've
1954 * loaded is to attempt to access .cssRules and wait for that to stop throwing an exception. But
1955 * cross-domain stylesheets never allow .cssRules to be accessed even after they have loaded.
1957 * The workaround is to change all `<link href="...">` tags to `<style>@import url(...)</style>`
1958 * tags. Because `@import` is blocking, Chrome won't add the stylesheet to document.styleSheets
1959 * until the `@import` has finished, and Firefox won't allow .cssRules to be accessed until the
1960 * `@import` has finished. And because the contents of the `<style>` tag are from the same origin,
1961 * accessing .cssRules is allowed.
1963 * However, now that we control the styles we're injecting, we might as well do away with
1964 * browser-specific polling hacks like document.styleSheets and .cssRules, and instead inject
1965 * `<style>@import url(...); #foo { font-family: someValue; }</style>`, then create `<div id="foo">`
1966 * and wait for its font-family to change to someValue. Because `@import` is blocking, the
1967 * font-family rule is not applied until after the `@import` finishes.
1969 * All this stylesheet injection and polling magic is in #transplantStyles.
1971 * @return {jQuery.Promise} Promise resolved when loading is complete
1974 OO
.ui
.Window
.prototype.load = function () {
1975 var sub
, doc
, loading
,
1978 // Non-isolated windows are already "loaded"
1979 if ( !this.loading
&& !this.isolated
) {
1980 this.loading
= $.Deferred().resolve();
1982 // Set initialized state after so sub-classes aren't confused by it being set by calling
1983 // their parent initialize method
1984 this.initialized
= true;
1987 // Return existing promise if already loading or loaded
1988 if ( this.loading
) {
1989 return this.loading
.promise();
1993 loading
= this.loading
= $.Deferred();
1994 sub
= this.$iframe
.prop( 'contentWindow' );
1997 // Initialize contents
2002 '<body class="oo-ui-window-isolated oo-ui-' + this.dir
+ '"' +
2003 ' style="direction:' + this.dir
+ ';" dir="' + this.dir
+ '">' +
2004 '<div class="oo-ui-window-content"></div>' +
2011 this.$ = OO
.ui
.Element
.getJQuery( doc
, this.$element
);
2012 this.$content
= this.$( '.oo-ui-window-content' ).attr( 'tabIndex', 0 );
2013 this.$document
= this.$( doc
);
2016 this.constructor.static.transplantStyles( this.getElementDocument(), this.$document
[0] )
2017 .always( function () {
2018 // Initialize isolated windows
2020 // Set initialized state after so sub-classes aren't confused by it being set by calling
2021 // their parent initialize method
2022 win
.initialized
= true;
2023 // Undo the visibility: hidden; hack and apply display: none;
2024 // We can do this safely now that the iframe has initialized
2025 // (don't do this from within #initialize because it has to happen
2026 // after the all subclasses have been handled as well).
2027 win
.toggle( win
.isVisible() );
2032 return loading
.promise();
2036 * Base class for all dialogs.
2039 * - Manage the window (open and close, etc.).
2040 * - Store the internal name and display title.
2041 * - A stack to track one or more pending actions.
2042 * - Manage a set of actions that can be performed.
2043 * - Configure and create action widgets.
2046 * - Close the dialog with Escape key.
2047 * - Visually lock the dialog while an action is in
2048 * progress (aka "pending").
2050 * Subclass responsibilities:
2051 * - Display the title somewhere.
2052 * - Add content to the dialog.
2053 * - Provide a UI to close the dialog.
2054 * - Display the action widgets somewhere.
2058 * @extends OO.ui.Window
2061 * @param {Object} [config] Configuration options
2063 OO
.ui
.Dialog
= function OoUiDialog( config
) {
2064 // Parent constructor
2065 OO
.ui
.Dialog
.super.call( this, config
);
2068 this.actions
= new OO
.ui
.ActionSet();
2069 this.attachedActions
= [];
2070 this.currentAction
= null;
2074 this.actions
.connect( this, {
2075 click
: 'onActionClick',
2076 resize
: 'onActionResize',
2077 change
: 'onActionsChange'
2082 .addClass( 'oo-ui-dialog' )
2083 .attr( 'role', 'dialog' );
2088 OO
.inheritClass( OO
.ui
.Dialog
, OO
.ui
.Window
);
2090 /* Static Properties */
2093 * Symbolic name of dialog.
2098 * @property {string}
2100 OO
.ui
.Dialog
.static.name
= '';
2108 * @property {jQuery|string|Function} Label nodes, text or a function that returns nodes or text
2110 OO
.ui
.Dialog
.static.title
= '';
2113 * List of OO.ui.ActionWidget configuration options.
2117 * @property {Object[]}
2119 OO
.ui
.Dialog
.static.actions
= [];
2122 * Close dialog when the escape key is pressed.
2127 * @property {boolean}
2129 OO
.ui
.Dialog
.static.escapable
= true;
2134 * Handle frame document key down events.
2136 * @param {jQuery.Event} e Key down event
2138 OO
.ui
.Dialog
.prototype.onDocumentKeyDown = function ( e
) {
2139 if ( e
.which
=== OO
.ui
.Keys
.ESCAPE
) {
2146 * Handle action resized events.
2148 * @param {OO.ui.ActionWidget} action Action that was resized
2150 OO
.ui
.Dialog
.prototype.onActionResize = function () {
2151 // Override in subclass
2155 * Handle action click events.
2157 * @param {OO.ui.ActionWidget} action Action that was clicked
2159 OO
.ui
.Dialog
.prototype.onActionClick = function ( action
) {
2160 if ( !this.isPending() ) {
2161 this.currentAction
= action
;
2162 this.executeAction( action
.getAction() );
2167 * Handle actions change event.
2169 OO
.ui
.Dialog
.prototype.onActionsChange = function () {
2170 this.detachActions();
2171 if ( !this.isClosing() ) {
2172 this.attachActions();
2177 * Check if input is pending.
2181 OO
.ui
.Dialog
.prototype.isPending = function () {
2182 return !!this.pending
;
2186 * Get set of actions.
2188 * @return {OO.ui.ActionSet}
2190 OO
.ui
.Dialog
.prototype.getActions = function () {
2191 return this.actions
;
2195 * Get a process for taking action.
2197 * When you override this method, you can add additional accept steps to the process the parent
2198 * method provides using the 'first' and 'next' methods.
2201 * @param {string} [action] Symbolic name of action
2202 * @return {OO.ui.Process} Action process
2204 OO
.ui
.Dialog
.prototype.getActionProcess = function ( action
) {
2205 return new OO
.ui
.Process()
2206 .next( function () {
2208 // An empty action always closes the dialog without data, which should always be
2209 // safe and make no changes
2218 * @param {Object} [data] Dialog opening data
2219 * @param {jQuery|string|Function|null} [data.label] Dialog label, omit to use #static-label
2220 * @param {Object[]} [data.actions] List of OO.ui.ActionWidget configuration options for each
2221 * action item, omit to use #static-actions
2223 OO
.ui
.Dialog
.prototype.getSetupProcess = function ( data
) {
2227 return OO
.ui
.Dialog
.super.prototype.getSetupProcess
.call( this, data
)
2228 .next( function () {
2231 config
= this.constructor.static,
2232 actions
= data
.actions
!== undefined ? data
.actions
: config
.actions
;
2234 this.title
.setLabel(
2235 data
.title
!== undefined ? data
.title
: this.constructor.static.title
2237 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
2239 new OO
.ui
.ActionWidget( $.extend( { $: this.$ }, actions
[i
] ) )
2242 this.actions
.add( items
);
2249 OO
.ui
.Dialog
.prototype.getTeardownProcess = function ( data
) {
2251 return OO
.ui
.Dialog
.super.prototype.getTeardownProcess
.call( this, data
)
2252 .first( function () {
2253 this.actions
.clear();
2254 this.currentAction
= null;
2261 OO
.ui
.Dialog
.prototype.initialize = function () {
2263 OO
.ui
.Dialog
.super.prototype.initialize
.call( this );
2266 this.title
= new OO
.ui
.LabelWidget( { $: this.$ } );
2269 if ( this.constructor.static.escapable
) {
2270 this.$document
.on( 'keydown', OO
.ui
.bind( this.onDocumentKeyDown
, this ) );
2274 this.$content
.addClass( 'oo-ui-dialog-content' );
2278 * Attach action actions.
2280 OO
.ui
.Dialog
.prototype.attachActions = function () {
2281 // Remember the list of potentially attached actions
2282 this.attachedActions
= this.actions
.get();
2286 * Detach action actions.
2290 OO
.ui
.Dialog
.prototype.detachActions = function () {
2293 // Detach all actions that may have been previously attached
2294 for ( i
= 0, len
= this.attachedActions
.length
; i
< len
; i
++ ) {
2295 this.attachedActions
[i
].$element
.detach();
2297 this.attachedActions
= [];
2301 * Execute an action.
2303 * @param {string} action Symbolic name of action to execute
2304 * @return {jQuery.Promise} Promise resolved when action completes, rejected if it fails
2306 OO
.ui
.Dialog
.prototype.executeAction = function ( action
) {
2308 return this.getActionProcess( action
).execute()
2309 .always( OO
.ui
.bind( this.popPending
, this ) );
2313 * Increase the pending stack.
2317 OO
.ui
.Dialog
.prototype.pushPending = function () {
2318 if ( this.pending
=== 0 ) {
2319 this.$content
.addClass( 'oo-ui-actionDialog-content-pending' );
2320 this.$head
.addClass( 'oo-ui-texture-pending' );
2328 * Reduce the pending stack.
2334 OO
.ui
.Dialog
.prototype.popPending = function () {
2335 if ( this.pending
=== 1 ) {
2336 this.$content
.removeClass( 'oo-ui-actionDialog-content-pending' );
2337 this.$head
.removeClass( 'oo-ui-texture-pending' );
2339 this.pending
= Math
.max( 0, this.pending
- 1 );
2345 * Collection of windows.
2348 * @extends OO.ui.Element
2349 * @mixins OO.EventEmitter
2351 * Managed windows are mutually exclusive. If a window is opened while there is a current window
2352 * already opening or opened, the current window will be closed without data. Empty closing data
2353 * should always result in the window being closed without causing constructive or destructive
2356 * As a window is opened and closed, it passes through several stages and the manager emits several
2357 * corresponding events.
2359 * - {@link #openWindow} or {@link OO.ui.Window#open} methods are used to start opening
2360 * - {@link #event-opening} is emitted with `opening` promise
2361 * - {@link #getSetupDelay} is called the returned value is used to time a pause in execution
2362 * - {@link OO.ui.Window#getSetupProcess} method is called on the window and its result executed
2363 * - `setup` progress notification is emitted from opening promise
2364 * - {@link #getReadyDelay} is called the returned value is used to time a pause in execution
2365 * - {@link OO.ui.Window#getReadyProcess} method is called on the window and its result executed
2366 * - `ready` progress notification is emitted from opening promise
2367 * - `opening` promise is resolved with `opened` promise
2368 * - Window is now open
2370 * - {@link #closeWindow} or {@link OO.ui.Window#close} methods are used to start closing
2371 * - `opened` promise is resolved with `closing` promise
2372 * - {@link #event-closing} is emitted with `closing` promise
2373 * - {@link #getHoldDelay} is called the returned value is used to time a pause in execution
2374 * - {@link OO.ui.Window#getHoldProcess} method is called on the window and its result executed
2375 * - `hold` progress notification is emitted from opening promise
2376 * - {@link #getTeardownDelay} is called the returned value is used to time a pause in execution
2377 * - {@link OO.ui.Window#getTeardownProcess} method is called on the window and its result executed
2378 * - `teardown` progress notification is emitted from opening promise
2379 * - Closing promise is resolved
2380 * - Window is now closed
2383 * @param {Object} [config] Configuration options
2384 * @cfg {boolean} [isolate] Configure managed windows to isolate their content using inline frames
2385 * @cfg {OO.Factory} [factory] Window factory to use for automatic instantiation
2386 * @cfg {boolean} [modal=true] Prevent interaction outside the dialog
2388 OO
.ui
.WindowManager
= function OoUiWindowManager( config
) {
2389 // Configuration initialization
2390 config
= config
|| {};
2392 // Parent constructor
2393 OO
.ui
.WindowManager
.super.call( this, config
);
2395 // Mixin constructors
2396 OO
.EventEmitter
.call( this );
2399 this.factory
= config
.factory
;
2400 this.modal
= config
.modal
=== undefined || !!config
.modal
;
2401 this.isolate
= !!config
.isolate
;
2403 this.opening
= null;
2405 this.closing
= null;
2406 this.preparingToOpen
= null;
2407 this.preparingToClose
= null;
2409 this.currentWindow
= null;
2410 this.$ariaHidden
= null;
2411 this.requestedSize
= null;
2412 this.onWindowResizeTimeout
= null;
2413 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
2414 this.afterWindowResizeHandler
= OO
.ui
.bind( this.afterWindowResize
, this );
2415 this.onWindowMouseWheelHandler
= OO
.ui
.bind( this.onWindowMouseWheel
, this );
2416 this.onDocumentKeyDownHandler
= OO
.ui
.bind( this.onDocumentKeyDown
, this );
2420 .addClass( 'oo-ui-windowManager' )
2421 .toggleClass( 'oo-ui-windowManager-modal', this.modal
);
2426 OO
.inheritClass( OO
.ui
.WindowManager
, OO
.ui
.Element
);
2427 OO
.mixinClass( OO
.ui
.WindowManager
, OO
.EventEmitter
);
2432 * Window is opening.
2434 * Fired when the window begins to be opened.
2437 * @param {OO.ui.Window} win Window that's being opened
2438 * @param {jQuery.Promise} opening Promise resolved when window is opened; when the promise is
2439 * resolved the first argument will be a promise which will be resolved when the window begins
2440 * closing, the second argument will be the opening data; progress notifications will be fired on
2441 * the promise for `setup` and `ready` when those processes are completed respectively.
2442 * @param {Object} data Window opening data
2446 * Window is closing.
2448 * Fired when the window begins to be closed.
2451 * @param {OO.ui.Window} win Window that's being closed
2452 * @param {jQuery.Promise} opening Promise resolved when window is closed; when the promise
2453 * is resolved the first argument will be a the closing data; progress notifications will be fired
2454 * on the promise for `hold` and `teardown` when those processes are completed respectively.
2455 * @param {Object} data Window closing data
2458 /* Static Properties */
2461 * Map of symbolic size names and CSS properties.
2465 * @property {Object}
2467 OO
.ui
.WindowManager
.static.sizes
= {
2478 // These can be non-numeric because they are never used in calculations
2485 * Symbolic name of default size.
2487 * Default size is used if the window's requested size is not recognized.
2491 * @property {string}
2493 OO
.ui
.WindowManager
.static.defaultSize
= 'medium';
2498 * Handle window resize events.
2500 * @param {jQuery.Event} e Window resize event
2502 OO
.ui
.WindowManager
.prototype.onWindowResize = function () {
2503 clearTimeout( this.onWindowResizeTimeout
);
2504 this.onWindowResizeTimeout
= setTimeout( this.afterWindowResizeHandler
, 200 );
2508 * Handle window resize events.
2510 * @param {jQuery.Event} e Window resize event
2512 OO
.ui
.WindowManager
.prototype.afterWindowResize = function () {
2513 if ( this.currentWindow
) {
2514 this.updateWindowSize( this.currentWindow
);
2519 * Handle window mouse wheel events.
2521 * @param {jQuery.Event} e Mouse wheel event
2523 OO
.ui
.WindowManager
.prototype.onWindowMouseWheel = function () {
2528 * Handle document key down events.
2530 * @param {jQuery.Event} e Key down event
2532 OO
.ui
.WindowManager
.prototype.onDocumentKeyDown = function ( e
) {
2533 switch ( e
.which
) {
2534 case OO
.ui
.Keys
.PAGEUP
:
2535 case OO
.ui
.Keys
.PAGEDOWN
:
2536 case OO
.ui
.Keys
.END
:
2537 case OO
.ui
.Keys
.HOME
:
2538 case OO
.ui
.Keys
.LEFT
:
2540 case OO
.ui
.Keys
.RIGHT
:
2541 case OO
.ui
.Keys
.DOWN
:
2542 // Prevent any key events that might cause scrolling
2548 * Check if window is opening.
2550 * @return {boolean} Window is opening
2552 OO
.ui
.WindowManager
.prototype.isOpening = function ( win
) {
2553 return win
=== this.currentWindow
&& !!this.opening
&& this.opening
.state() === 'pending';
2557 * Check if window is closing.
2559 * @return {boolean} Window is closing
2561 OO
.ui
.WindowManager
.prototype.isClosing = function ( win
) {
2562 return win
=== this.currentWindow
&& !!this.closing
&& this.closing
.state() === 'pending';
2566 * Check if window is opened.
2568 * @return {boolean} Window is opened
2570 OO
.ui
.WindowManager
.prototype.isOpened = function ( win
) {
2571 return win
=== this.currentWindow
&& !!this.opened
&& this.opened
.state() === 'pending';
2575 * Check if window contents should be isolated.
2577 * Window content isolation is done using inline frames.
2579 * @return {boolean} Window contents should be isolated
2581 OO
.ui
.WindowManager
.prototype.shouldIsolate = function () {
2582 return this.isolate
;
2586 * Check if a window is being managed.
2588 * @param {OO.ui.Window} win Window to check
2589 * @return {boolean} Window is being managed
2591 OO
.ui
.WindowManager
.prototype.hasWindow = function ( win
) {
2594 for ( name
in this.windows
) {
2595 if ( this.windows
[name
] === win
) {
2604 * Get the number of milliseconds to wait between beginning opening and executing setup process.
2606 * @param {OO.ui.Window} win Window being opened
2607 * @param {Object} [data] Window opening data
2608 * @return {number} Milliseconds to wait
2610 OO
.ui
.WindowManager
.prototype.getSetupDelay = function () {
2615 * Get the number of milliseconds to wait between finishing setup and executing ready process.
2617 * @param {OO.ui.Window} win Window being opened
2618 * @param {Object} [data] Window opening data
2619 * @return {number} Milliseconds to wait
2621 OO
.ui
.WindowManager
.prototype.getReadyDelay = function () {
2626 * Get the number of milliseconds to wait between beginning closing and executing hold process.
2628 * @param {OO.ui.Window} win Window being closed
2629 * @param {Object} [data] Window closing data
2630 * @return {number} Milliseconds to wait
2632 OO
.ui
.WindowManager
.prototype.getHoldDelay = function () {
2637 * Get the number of milliseconds to wait between finishing hold and executing teardown process.
2639 * @param {OO.ui.Window} win Window being closed
2640 * @param {Object} [data] Window closing data
2641 * @return {number} Milliseconds to wait
2643 OO
.ui
.WindowManager
.prototype.getTeardownDelay = function () {
2644 return this.modal
? 250 : 0;
2648 * Get managed window by symbolic name.
2650 * If window is not yet instantiated, it will be instantiated and added automatically.
2652 * @param {string} name Symbolic window name
2653 * @return {jQuery.Promise} Promise resolved with matching window, or rejected with an OO.ui.Error
2654 * @throws {Error} If the symbolic name is unrecognized by the factory
2655 * @throws {Error} If the symbolic name unrecognized as a managed window
2657 OO
.ui
.WindowManager
.prototype.getWindow = function ( name
) {
2658 var deferred
= $.Deferred(),
2659 win
= this.windows
[name
];
2661 if ( !( win
instanceof OO
.ui
.Window
) ) {
2662 if ( this.factory
) {
2663 if ( !this.factory
.lookup( name
) ) {
2664 deferred
.reject( new OO
.ui
.Error(
2665 'Cannot auto-instantiate window: symbolic name is unrecognized by the factory'
2668 win
= this.factory
.create( name
, this, { $: this.$ } );
2669 this.addWindows( [ win
] );
2670 deferred
.resolve( win
);
2673 deferred
.reject( new OO
.ui
.Error(
2674 'Cannot get unmanaged window: symbolic name unrecognized as a managed window'
2678 deferred
.resolve( win
);
2681 return deferred
.promise();
2685 * Get current window.
2687 * @return {OO.ui.Window|null} Currently opening/opened/closing window
2689 OO
.ui
.WindowManager
.prototype.getCurrentWindow = function () {
2690 return this.currentWindow
;
2696 * @param {OO.ui.Window|string} win Window object or symbolic name of window to open
2697 * @param {Object} [data] Window opening data
2698 * @return {jQuery.Promise} Promise resolved when window is done opening; see {@link #event-opening}
2699 * for more details about the `opening` promise
2702 OO
.ui
.WindowManager
.prototype.openWindow = function ( win
, data
) {
2705 opening
= $.Deferred();
2707 // Argument handling
2708 if ( typeof win
=== 'string' ) {
2709 return this.getWindow( win
).then( function ( win
) {
2710 return manager
.openWindow( win
, data
);
2715 if ( !this.hasWindow( win
) ) {
2716 opening
.reject( new OO
.ui
.Error(
2717 'Cannot open window: window is not attached to manager'
2719 } else if ( this.preparingToOpen
|| this.opening
|| this.opened
) {
2720 opening
.reject( new OO
.ui
.Error(
2721 'Cannot open window: another window is opening or open'
2726 if ( opening
.state() !== 'rejected' ) {
2727 // Begin loading the window if it's not loading or loaded already - may take noticable time
2728 // and we want to do this in paralell with any other preparatory actions
2729 if ( !win
.isLoading() && !win
.isLoaded() ) {
2730 // Finish initializing the window (must be done after manager is attached to DOM)
2731 win
.setManager( this );
2732 preparing
.push( win
.load() );
2735 if ( this.closing
) {
2736 // If a window is currently closing, wait for it to complete
2737 preparing
.push( this.closing
);
2740 this.preparingToOpen
= $.when
.apply( $, preparing
);
2741 // Ensure handlers get called after preparingToOpen is set
2742 this.preparingToOpen
.done( function () {
2743 if ( manager
.modal
) {
2744 manager
.toggleGlobalEvents( true );
2745 manager
.toggleAriaIsolation( true );
2747 manager
.currentWindow
= win
;
2748 manager
.opening
= opening
;
2749 manager
.preparingToOpen
= null;
2750 manager
.emit( 'opening', win
, opening
, data
);
2751 setTimeout( function () {
2752 win
.setup( data
).then( function () {
2753 manager
.updateWindowSize( win
);
2754 manager
.opening
.notify( { state
: 'setup' } );
2755 setTimeout( function () {
2756 win
.ready( data
).then( function () {
2757 manager
.updateWindowSize( win
);
2758 manager
.opening
.notify( { state
: 'ready' } );
2759 manager
.opening
= null;
2760 manager
.opened
= $.Deferred();
2761 opening
.resolve( manager
.opened
.promise(), data
);
2763 }, manager
.getReadyDelay() );
2765 }, manager
.getSetupDelay() );
2769 return opening
.promise();
2775 * @param {OO.ui.Window|string} win Window object or symbolic name of window to close
2776 * @param {Object} [data] Window closing data
2777 * @return {jQuery.Promise} Promise resolved when window is done closing; see {@link #event-closing}
2778 * for more details about the `closing` promise
2779 * @throws {Error} If no window by that name is being managed
2782 OO
.ui
.WindowManager
.prototype.closeWindow = function ( win
, data
) {
2785 closing
= $.Deferred(),
2786 opened
= this.opened
;
2788 // Argument handling
2789 if ( typeof win
=== 'string' ) {
2790 win
= this.windows
[win
];
2791 } else if ( !this.hasWindow( win
) ) {
2797 closing
.reject( new OO
.ui
.Error(
2798 'Cannot close window: window is not attached to manager'
2800 } else if ( win
!== this.currentWindow
) {
2801 closing
.reject( new OO
.ui
.Error(
2802 'Cannot close window: window already closed with different data'
2804 } else if ( this.preparingToClose
|| this.closing
) {
2805 closing
.reject( new OO
.ui
.Error(
2806 'Cannot close window: window already closing with different data'
2811 if ( closing
.state() !== 'rejected' ) {
2812 if ( this.opening
) {
2813 // If the window is currently opening, close it when it's done
2814 preparing
.push( this.opening
);
2817 this.preparingToClose
= $.when
.apply( $, preparing
);
2818 // Ensure handlers get called after preparingToClose is set
2819 this.preparingToClose
.done( function () {
2820 manager
.closing
= closing
;
2821 manager
.preparingToClose
= null;
2822 manager
.emit( 'closing', win
, closing
, data
);
2823 manager
.opened
= null;
2824 opened
.resolve( closing
.promise(), data
);
2825 setTimeout( function () {
2826 win
.hold( data
).then( function () {
2827 closing
.notify( { state
: 'hold' } );
2828 setTimeout( function () {
2829 win
.teardown( data
).then( function () {
2830 closing
.notify( { state
: 'teardown' } );
2831 if ( manager
.modal
) {
2832 manager
.toggleGlobalEvents( false );
2833 manager
.toggleAriaIsolation( false );
2835 manager
.closing
= null;
2836 manager
.currentWindow
= null;
2837 closing
.resolve( data
);
2839 }, manager
.getTeardownDelay() );
2841 }, manager
.getHoldDelay() );
2845 return closing
.promise();
2851 * @param {Object.<string,OO.ui.Window>|OO.ui.Window[]} windows Windows to add
2852 * @throws {Error} If one of the windows being added without an explicit symbolic name does not have
2853 * a statically configured symbolic name
2855 OO
.ui
.WindowManager
.prototype.addWindows = function ( windows
) {
2856 var i
, len
, win
, name
, list
;
2858 if ( $.isArray( windows
) ) {
2859 // Convert to map of windows by looking up symbolic names from static configuration
2861 for ( i
= 0, len
= windows
.length
; i
< len
; i
++ ) {
2862 name
= windows
[i
].constructor.static.name
;
2863 if ( typeof name
!== 'string' ) {
2864 throw new Error( 'Cannot add window' );
2866 list
[name
] = windows
[i
];
2868 } else if ( $.isPlainObject( windows
) ) {
2873 for ( name
in list
) {
2875 this.windows
[name
] = win
;
2876 this.$element
.append( win
.$element
);
2883 * Windows will be closed before they are removed.
2885 * @param {string} name Symbolic name of window to remove
2886 * @return {jQuery.Promise} Promise resolved when window is closed and removed
2887 * @throws {Error} If windows being removed are not being managed
2889 OO
.ui
.WindowManager
.prototype.removeWindows = function ( names
) {
2890 var i
, len
, win
, name
,
2893 cleanup = function ( name
, win
) {
2894 delete manager
.windows
[name
];
2895 win
.$element
.detach();
2898 for ( i
= 0, len
= names
.length
; i
< len
; i
++ ) {
2900 win
= this.windows
[name
];
2902 throw new Error( 'Cannot remove window' );
2904 promises
.push( this.closeWindow( name
).then( OO
.ui
.bind( cleanup
, null, name
, win
) ) );
2907 return $.when
.apply( $, promises
);
2911 * Remove all windows.
2913 * Windows will be closed before they are removed.
2915 * @return {jQuery.Promise} Promise resolved when all windows are closed and removed
2917 OO
.ui
.WindowManager
.prototype.clearWindows = function () {
2918 return this.removeWindows( Object
.keys( this.windows
) );
2924 * Fullscreen mode will be used if the dialog is too wide to fit in the screen.
2928 OO
.ui
.WindowManager
.prototype.updateWindowSize = function ( win
) {
2929 // Bypass for non-current, and thus invisible, windows
2930 if ( win
!== this.currentWindow
) {
2934 var viewport
= OO
.ui
.Element
.getDimensions( win
.getElementWindow() ),
2935 sizes
= this.constructor.static.sizes
,
2936 size
= win
.getSize();
2938 if ( !sizes
[size
] ) {
2939 size
= this.constructor.static.defaultSize
;
2941 if ( size
!== 'full' && viewport
.rect
.right
- viewport
.rect
.left
< sizes
[size
].width
) {
2945 this.$element
.toggleClass( 'oo-ui-windowManager-fullscreen', size
=== 'full' );
2946 this.$element
.toggleClass( 'oo-ui-windowManager-floating', size
!== 'full' );
2947 win
.setDimensions( sizes
[size
] );
2953 * Bind or unbind global events for scrolling.
2955 * @param {boolean} [on] Bind global events
2958 OO
.ui
.WindowManager
.prototype.toggleGlobalEvents = function ( on
) {
2959 on
= on
=== undefined ? !!this.globalEvents
: !!on
;
2962 if ( !this.globalEvents
) {
2963 this.$( this.getElementDocument() ).on( {
2964 // Prevent scrolling by keys in top-level window
2965 keydown
: this.onDocumentKeyDownHandler
2967 this.$( this.getElementWindow() ).on( {
2968 // Prevent scrolling by wheel in top-level window
2969 mousewheel
: this.onWindowMouseWheelHandler
,
2970 // Start listening for top-level window dimension changes
2971 'orientationchange resize': this.onWindowResizeHandler
2973 this.globalEvents
= true;
2975 } else if ( this.globalEvents
) {
2976 // Unbind global events
2977 this.$( this.getElementDocument() ).off( {
2978 // Allow scrolling by keys in top-level window
2979 keydown
: this.onDocumentKeyDownHandler
2981 this.$( this.getElementWindow() ).off( {
2982 // Allow scrolling by wheel in top-level window
2983 mousewheel
: this.onWindowMouseWheelHandler
,
2984 // Stop listening for top-level window dimension changes
2985 'orientationchange resize': this.onWindowResizeHandler
2987 this.globalEvents
= false;
2994 * Toggle screen reader visibility of content other than the window manager.
2996 * @param {boolean} [isolate] Make only the window manager visible to screen readers
2999 OO
.ui
.WindowManager
.prototype.toggleAriaIsolation = function ( isolate
) {
3000 isolate
= isolate
=== undefined ? !this.$ariaHidden
: !!isolate
;
3003 if ( !this.$ariaHidden
) {
3004 // Hide everything other than the window manager from screen readers
3005 this.$ariaHidden
= $( 'body' )
3007 .not( this.$element
.parentsUntil( 'body' ).last() )
3008 .attr( 'aria-hidden', '' );
3010 } else if ( this.$ariaHidden
) {
3011 // Restore screen reader visiblity
3012 this.$ariaHidden
.removeAttr( 'aria-hidden' );
3013 this.$ariaHidden
= null;
3020 * Destroy window manager.
3022 * Windows will not be closed, only removed from the DOM.
3024 OO
.ui
.WindowManager
.prototype.destroy = function () {
3025 this.toggleGlobalEvents( false );
3026 this.toggleAriaIsolation( false );
3027 this.$element
.remove();
3035 * @param {string|jQuery} message Description of error
3036 * @param {Object} [config] Configuration options
3037 * @cfg {boolean} [recoverable=true] Error is recoverable
3039 OO
.ui
.Error
= function OoUiElement( message
, config
) {
3040 // Configuration initialization
3041 config
= config
|| {};
3044 this.message
= message
instanceof jQuery
? message
: String( message
);
3045 this.recoverable
= config
.recoverable
=== undefined || !!config
.recoverable
;
3050 OO
.initClass( OO
.ui
.Error
);
3055 * Check if error can be recovered from.
3057 * @return {boolean} Error is recoverable
3059 OO
.ui
.Error
.prototype.isRecoverable = function () {
3060 return this.recoverable
;
3064 * Get error message as DOM nodes.
3066 * @return {jQuery} Error message in DOM nodes
3068 OO
.ui
.Error
.prototype.getMessage = function () {
3069 return this.message
instanceof jQuery
?
3070 this.message
.clone() :
3071 $( '<div>' ).text( this.message
).contents();
3075 * Get error message as text.
3077 * @return {string} Error message
3079 OO
.ui
.Error
.prototype.getMessageText = function () {
3080 return this.message
instanceof jQuery
? this.message
.text() : this.message
;
3084 * A list of functions, called in sequence.
3086 * If a function added to a process returns boolean false the process will stop; if it returns an
3087 * object with a `promise` method the process will use the promise to either continue to the next
3088 * step when the promise is resolved or stop when the promise is rejected.
3093 * @param {number|jQuery.Promise|Function} step Time to wait, promise to wait for or function to
3094 * call, see #createStep for more information
3095 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3097 * @return {Object} Step object, with `callback` and `context` properties
3099 OO
.ui
.Process = function ( step
, context
) {
3104 if ( step
!== undefined ) {
3105 this.next( step
, context
);
3111 OO
.initClass( OO
.ui
.Process
);
3116 * Start the process.
3118 * @return {jQuery.Promise} Promise that is resolved when all steps have completed or rejected when
3119 * any of the steps return boolean false or a promise which gets rejected; upon stopping the
3120 * process, the remaining steps will not be taken
3122 OO
.ui
.Process
.prototype.execute = function () {
3123 var i
, len
, promise
;
3126 * Continue execution.
3129 * @param {Array} step A function and the context it should be called in
3130 * @return {Function} Function that continues the process
3132 function proceed( step
) {
3133 return function () {
3134 // Execute step in the correct context
3136 result
= step
.callback
.call( step
.context
);
3138 if ( result
=== false ) {
3139 // Use rejected promise for boolean false results
3140 return $.Deferred().reject( [] ).promise();
3142 if ( typeof result
=== 'number' ) {
3144 throw new Error( 'Cannot go back in time: flux capacitor is out of service' );
3146 // Use a delayed promise for numbers, expecting them to be in milliseconds
3147 deferred
= $.Deferred();
3148 setTimeout( deferred
.resolve
, result
);
3149 return deferred
.promise();
3151 if ( result
instanceof OO
.ui
.Error
) {
3152 // Use rejected promise for error
3153 return $.Deferred().reject( [ result
] ).promise();
3155 if ( $.isArray( result
) && result
.length
&& result
[0] instanceof OO
.ui
.Error
) {
3156 // Use rejected promise for list of errors
3157 return $.Deferred().reject( result
).promise();
3159 // Duck-type the object to see if it can produce a promise
3160 if ( result
&& $.isFunction( result
.promise
) ) {
3161 // Use a promise generated from the result
3162 return result
.promise();
3164 // Use resolved promise for other results
3165 return $.Deferred().resolve().promise();
3169 if ( this.steps
.length
) {
3170 // Generate a chain reaction of promises
3171 promise
= proceed( this.steps
[0] )();
3172 for ( i
= 1, len
= this.steps
.length
; i
< len
; i
++ ) {
3173 promise
= promise
.then( proceed( this.steps
[i
] ) );
3176 promise
= $.Deferred().resolve().promise();
3183 * Create a process step.
3186 * @param {number|jQuery.Promise|Function} step
3188 * - Number of milliseconds to wait; or
3189 * - Promise to wait to be resolved; or
3190 * - Function to execute
3191 * - If it returns boolean false the process will stop
3192 * - If it returns an object with a `promise` method the process will use the promise to either
3193 * continue to the next step when the promise is resolved or stop when the promise is rejected
3194 * - If it returns a number, the process will wait for that number of milliseconds before
3196 * @param {Object} [context=null] Context to call the step function in, ignored if step is a number
3198 * @return {Object} Step object, with `callback` and `context` properties
3200 OO
.ui
.Process
.prototype.createStep = function ( step
, context
) {
3201 if ( typeof step
=== 'number' || $.isFunction( step
.promise
) ) {
3203 callback: function () {
3209 if ( $.isFunction( step
) ) {
3215 throw new Error( 'Cannot create process step: number, promise or function expected' );
3219 * Add step to the beginning of the process.
3221 * @inheritdoc #createStep
3222 * @return {OO.ui.Process} this
3225 OO
.ui
.Process
.prototype.first = function ( step
, context
) {
3226 this.steps
.unshift( this.createStep( step
, context
) );
3231 * Add step to the end of the process.
3233 * @inheritdoc #createStep
3234 * @return {OO.ui.Process} this
3237 OO
.ui
.Process
.prototype.next = function ( step
, context
) {
3238 this.steps
.push( this.createStep( step
, context
) );
3243 * Factory for tools.
3246 * @extends OO.Factory
3249 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
3250 // Parent constructor
3251 OO
.ui
.ToolFactory
.super.call( this );
3256 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
3261 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
3262 var i
, len
, included
, promoted
, demoted
,
3266 // Collect included and not excluded tools
3267 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
3270 promoted
= this.extract( promote
, used
);
3271 demoted
= this.extract( demote
, used
);
3274 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
3275 if ( !used
[included
[i
]] ) {
3276 auto
.push( included
[i
] );
3280 return promoted
.concat( auto
).concat( demoted
);
3284 * Get a flat list of names from a list of names or groups.
3286 * Tools can be specified in the following ways:
3288 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
3289 * - All tools in a group: `{ group: 'group-name' }`
3290 * - All tools: `'*'`
3293 * @param {Array|string} collection List of tools
3294 * @param {Object} [used] Object with names that should be skipped as properties; extracted
3295 * names will be added as properties
3296 * @return {string[]} List of extracted names
3298 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
3299 var i
, len
, item
, name
, tool
,
3302 if ( collection
=== '*' ) {
3303 for ( name
in this.registry
) {
3304 tool
= this.registry
[name
];
3306 // Only add tools by group name when auto-add is enabled
3307 tool
.static.autoAddToCatchall
&&
3308 // Exclude already used tools
3309 ( !used
|| !used
[name
] )
3317 } else if ( $.isArray( collection
) ) {
3318 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
3319 item
= collection
[i
];
3320 // Allow plain strings as shorthand for named tools
3321 if ( typeof item
=== 'string' ) {
3322 item
= { name
: item
};
3324 if ( OO
.isPlainObject( item
) ) {
3326 for ( name
in this.registry
) {
3327 tool
= this.registry
[name
];
3329 // Include tools with matching group
3330 tool
.static.group
=== item
.group
&&
3331 // Only add tools by group name when auto-add is enabled
3332 tool
.static.autoAddToGroup
&&
3333 // Exclude already used tools
3334 ( !used
|| !used
[name
] )
3342 // Include tools with matching name and exclude already used tools
3343 } else if ( item
.name
&& ( !used
|| !used
[item
.name
] ) ) {
3344 names
.push( item
.name
);
3346 used
[item
.name
] = true;
3356 * Factory for tool groups.
3359 * @extends OO.Factory
3362 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
3363 // Parent constructor
3364 OO
.Factory
.call( this );
3367 defaultClasses
= this.constructor.static.getDefaultClasses();
3369 // Register default toolgroups
3370 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
3371 this.register( defaultClasses
[i
] );
3377 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
3379 /* Static Methods */
3382 * Get a default set of classes to be registered on construction
3384 * @return {Function[]} Default classes
3386 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
3389 OO
.ui
.ListToolGroup
,
3395 * Element with a button.
3397 * Buttons are used for controls which can be clicked. They can be configured to use tab indexing
3398 * and access keys for accessibility purposes.
3404 * @param {Object} [config] Configuration options
3405 * @cfg {jQuery} [$button] Button node, assigned to #$button, omit to use a generated `<a>`
3406 * @cfg {boolean} [framed=true] Render button with a frame
3407 * @cfg {number} [tabIndex=0] Button's tab index, use null to have no tabIndex
3408 * @cfg {string} [accessKey] Button's access key
3410 OO
.ui
.ButtonElement
= function OoUiButtonElement( config
) {
3411 // Configuration initialization
3412 config
= config
|| {};
3415 this.$button
= null;
3417 this.tabIndex
= null;
3418 this.accessKey
= null;
3419 this.active
= false;
3420 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
3421 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
3424 this.$element
.addClass( 'oo-ui-buttonElement' );
3425 this.toggleFramed( config
.framed
=== undefined || config
.framed
);
3426 this.setTabIndex( config
.tabIndex
|| 0 );
3427 this.setAccessKey( config
.accessKey
);
3428 this.setButtonElement( config
.$button
|| this.$( '<a>' ) );
3433 OO
.initClass( OO
.ui
.ButtonElement
);
3435 /* Static Properties */
3438 * Cancel mouse down events.
3442 * @property {boolean}
3444 OO
.ui
.ButtonElement
.static.cancelButtonMouseDownEvents
= true;
3449 * Set the button element.
3451 * If an element is already set, it will be cleaned up before setting up the new element.
3453 * @param {jQuery} $button Element to use as button
3455 OO
.ui
.ButtonElement
.prototype.setButtonElement = function ( $button
) {
3456 if ( this.$button
) {
3458 .removeClass( 'oo-ui-buttonElement-button' )
3459 .removeAttr( 'role accesskey tabindex' )
3460 .off( this.onMouseDownHandler
);
3463 this.$button
= $button
3464 .addClass( 'oo-ui-buttonElement-button' )
3465 .attr( { role
: 'button', accesskey
: this.accessKey
, tabindex
: this.tabIndex
} )
3466 .on( 'mousedown', this.onMouseDownHandler
);
3470 * Handles mouse down events.
3472 * @param {jQuery.Event} e Mouse down event
3474 OO
.ui
.ButtonElement
.prototype.onMouseDown = function ( e
) {
3475 if ( this.isDisabled() || e
.which
!== 1 ) {
3478 // Remove the tab-index while the button is down to prevent the button from stealing focus
3479 this.$button
.removeAttr( 'tabindex' );
3480 this.$element
.addClass( 'oo-ui-buttonElement-pressed' );
3481 // Run the mouseup handler no matter where the mouse is when the button is let go, so we can
3482 // reliably reapply the tabindex and remove the pressed class
3483 this.getElementDocument().addEventListener( 'mouseup', this.onMouseUpHandler
, true );
3484 // Prevent change of focus unless specifically configured otherwise
3485 if ( this.constructor.static.cancelButtonMouseDownEvents
) {
3491 * Handles mouse up events.
3493 * @param {jQuery.Event} e Mouse up event
3495 OO
.ui
.ButtonElement
.prototype.onMouseUp = function ( e
) {
3496 if ( this.isDisabled() || e
.which
!== 1 ) {
3499 // Restore the tab-index after the button is up to restore the button's accesssibility
3500 this.$button
.attr( 'tabindex', this.tabIndex
);
3501 this.$element
.removeClass( 'oo-ui-buttonElement-pressed' );
3502 // Stop listening for mouseup, since we only needed this once
3503 this.getElementDocument().removeEventListener( 'mouseup', this.onMouseUpHandler
, true );
3509 * @param {boolean} [framed] Make button framed, omit to toggle
3512 OO
.ui
.ButtonElement
.prototype.toggleFramed = function ( framed
) {
3513 framed
= framed
=== undefined ? !this.framed
: !!framed
;
3514 if ( framed
!== this.framed
) {
3515 this.framed
= framed
;
3517 .toggleClass( 'oo-ui-buttonElement-frameless', !framed
)
3518 .toggleClass( 'oo-ui-buttonElement-framed', framed
);
3527 * @param {number|null} tabIndex Button's tab index, use null to remove
3530 OO
.ui
.ButtonElement
.prototype.setTabIndex = function ( tabIndex
) {
3531 tabIndex
= typeof tabIndex
=== 'number' && tabIndex
>= 0 ? tabIndex
: null;
3533 if ( this.tabIndex
!== tabIndex
) {
3534 if ( this.$button
) {
3535 if ( tabIndex
!== null ) {
3536 this.$button
.attr( 'tabindex', tabIndex
);
3538 this.$button
.removeAttr( 'tabindex' );
3541 this.tabIndex
= tabIndex
;
3550 * @param {string} accessKey Button's access key, use empty string to remove
3553 OO
.ui
.ButtonElement
.prototype.setAccessKey = function ( accessKey
) {
3554 accessKey
= typeof accessKey
=== 'string' && accessKey
.length
? accessKey
: null;
3556 if ( this.accessKey
!== accessKey
) {
3557 if ( this.$button
) {
3558 if ( accessKey
!== null ) {
3559 this.$button
.attr( 'accesskey', accessKey
);
3561 this.$button
.removeAttr( 'accesskey' );
3564 this.accessKey
= accessKey
;
3573 * @param {boolean} [value] Make button active
3576 OO
.ui
.ButtonElement
.prototype.setActive = function ( value
) {
3577 this.$element
.toggleClass( 'oo-ui-buttonElement-active', !!value
);
3582 * Element containing a sequence of child elements.
3588 * @param {Object} [config] Configuration options
3589 * @cfg {jQuery} [$group] Container node, assigned to #$group, omit to use a generated `<div>`
3591 OO
.ui
.GroupElement
= function OoUiGroupElement( config
) {
3593 config
= config
|| {};
3598 this.aggregateItemEvents
= {};
3601 this.setGroupElement( config
.$group
|| this.$( '<div>' ) );
3607 * Set the group element.
3609 * If an element is already set, items will be moved to the new element.
3611 * @param {jQuery} $group Element to use as group
3613 OO
.ui
.GroupElement
.prototype.setGroupElement = function ( $group
) {
3616 this.$group
= $group
;
3617 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3618 this.$group
.append( this.items
[i
].$element
);
3623 * Check if there are no items.
3625 * @return {boolean} Group is empty
3627 OO
.ui
.GroupElement
.prototype.isEmpty = function () {
3628 return !this.items
.length
;
3634 * @return {OO.ui.Element[]} Items
3636 OO
.ui
.GroupElement
.prototype.getItems = function () {
3637 return this.items
.slice( 0 );
3641 * Add an aggregate item event.
3643 * Aggregated events are listened to on each item and then emitted by the group under a new name,
3644 * and with an additional leading parameter containing the item that emitted the original event.
3645 * Other arguments that were emitted from the original event are passed through.
3647 * @param {Object.<string,string|null>} events Aggregate events emitted by group, keyed by item
3648 * event, use null value to remove aggregation
3649 * @throws {Error} If aggregation already exists
3651 OO
.ui
.GroupElement
.prototype.aggregate = function ( events
) {
3652 var i
, len
, item
, add
, remove
, itemEvent
, groupEvent
;
3654 for ( itemEvent
in events
) {
3655 groupEvent
= events
[itemEvent
];
3657 // Remove existing aggregated event
3658 if ( itemEvent
in this.aggregateItemEvents
) {
3659 // Don't allow duplicate aggregations
3661 throw new Error( 'Duplicate item event aggregation for ' + itemEvent
);
3663 // Remove event aggregation from existing items
3664 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3665 item
= this.items
[i
];
3666 if ( item
.connect
&& item
.disconnect
) {
3668 remove
[itemEvent
] = [ 'emit', groupEvent
, item
];
3669 item
.disconnect( this, remove
);
3672 // Prevent future items from aggregating event
3673 delete this.aggregateItemEvents
[itemEvent
];
3676 // Add new aggregate event
3678 // Make future items aggregate event
3679 this.aggregateItemEvents
[itemEvent
] = groupEvent
;
3680 // Add event aggregation to existing items
3681 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3682 item
= this.items
[i
];
3683 if ( item
.connect
&& item
.disconnect
) {
3685 add
[itemEvent
] = [ 'emit', groupEvent
, item
];
3686 item
.connect( this, add
);
3696 * Adding an existing item (by value) will move it.
3698 * @param {OO.ui.Element[]} items Item
3699 * @param {number} [index] Index to insert items at
3702 OO
.ui
.GroupElement
.prototype.addItems = function ( items
, index
) {
3703 var i
, len
, item
, event
, events
, currentIndex
,
3706 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3709 // Check if item exists then remove it first, effectively "moving" it
3710 currentIndex
= $.inArray( item
, this.items
);
3711 if ( currentIndex
>= 0 ) {
3712 this.removeItems( [ item
] );
3713 // Adjust index to compensate for removal
3714 if ( currentIndex
< index
) {
3719 if ( item
.connect
&& item
.disconnect
&& !$.isEmptyObject( this.aggregateItemEvents
) ) {
3721 for ( event
in this.aggregateItemEvents
) {
3722 events
[event
] = [ 'emit', this.aggregateItemEvents
[event
], item
];
3724 item
.connect( this, events
);
3726 item
.setElementGroup( this );
3727 itemElements
.push( item
.$element
.get( 0 ) );
3730 if ( index
=== undefined || index
< 0 || index
>= this.items
.length
) {
3731 this.$group
.append( itemElements
);
3732 this.items
.push
.apply( this.items
, items
);
3733 } else if ( index
=== 0 ) {
3734 this.$group
.prepend( itemElements
);
3735 this.items
.unshift
.apply( this.items
, items
);
3737 this.items
[index
].$element
.before( itemElements
);
3738 this.items
.splice
.apply( this.items
, [ index
, 0 ].concat( items
) );
3747 * Items will be detached, not removed, so they can be used later.
3749 * @param {OO.ui.Element[]} items Items to remove
3752 OO
.ui
.GroupElement
.prototype.removeItems = function ( items
) {
3753 var i
, len
, item
, index
, remove
, itemEvent
;
3755 // Remove specific items
3756 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
3758 index
= $.inArray( item
, this.items
);
3759 if ( index
!== -1 ) {
3761 item
.connect
&& item
.disconnect
&&
3762 !$.isEmptyObject( this.aggregateItemEvents
)
3765 if ( itemEvent
in this.aggregateItemEvents
) {
3766 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3768 item
.disconnect( this, remove
);
3770 item
.setElementGroup( null );
3771 this.items
.splice( index
, 1 );
3772 item
.$element
.detach();
3782 * Items will be detached, not removed, so they can be used later.
3786 OO
.ui
.GroupElement
.prototype.clearItems = function () {
3787 var i
, len
, item
, remove
, itemEvent
;
3790 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
3791 item
= this.items
[i
];
3793 item
.connect
&& item
.disconnect
&&
3794 !$.isEmptyObject( this.aggregateItemEvents
)
3797 if ( itemEvent
in this.aggregateItemEvents
) {
3798 remove
[itemEvent
] = [ 'emit', this.aggregateItemEvents
[itemEvent
], item
];
3800 item
.disconnect( this, remove
);
3802 item
.setElementGroup( null );
3803 item
.$element
.detach();
3811 * Element containing an icon.
3813 * Icons are graphics, about the size of normal text. They can be used to aid the user in locating
3814 * a control or convey information in a more space efficient way. Icons should rarely be used
3815 * without labels; such as in a toolbar where space is at a premium or within a context where the
3816 * meaning is very clear to the user.
3822 * @param {Object} [config] Configuration options
3823 * @cfg {jQuery} [$icon] Icon node, assigned to #$icon, omit to use a generated `<span>`
3824 * @cfg {Object|string} [icon=''] Symbolic icon name, or map of icon names keyed by language ID;
3825 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3827 * @cfg {string} [iconTitle] Icon title text or a function that returns text
3829 OO
.ui
.IconElement
= function OoUiIconElement( config
) {
3830 // Config intialization
3831 config
= config
|| {};
3836 this.iconTitle
= null;
3839 this.setIcon( config
.icon
|| this.constructor.static.icon
);
3840 this.setIconTitle( config
.iconTitle
|| this.constructor.static.iconTitle
);
3841 this.setIconElement( config
.$icon
|| this.$( '<span>' ) );
3846 OO
.initClass( OO
.ui
.IconElement
);
3848 /* Static Properties */
3853 * Value should be the unique portion of an icon CSS class name, such as 'up' for 'oo-ui-icon-up'.
3855 * For i18n purposes, this property can be an object containing a `default` icon name property and
3856 * additional icon names keyed by language code.
3858 * Example of i18n icon definition:
3859 * { default: 'bold-a', en: 'bold-b', de: 'bold-f' }
3863 * @property {Object|string} Symbolic icon name, or map of icon names keyed by language ID;
3864 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3867 OO
.ui
.IconElement
.static.icon
= null;
3874 * @property {string|Function|null} Icon title text, a function that returns text or null for no
3877 OO
.ui
.IconElement
.static.iconTitle
= null;
3882 * Set the icon element.
3884 * If an element is already set, it will be cleaned up before setting up the new element.
3886 * @param {jQuery} $icon Element to use as icon
3888 OO
.ui
.IconElement
.prototype.setIconElement = function ( $icon
) {
3891 .removeClass( 'oo-ui-iconElement-icon oo-ui-icon-' + this.icon
)
3892 .removeAttr( 'title' );
3896 .addClass( 'oo-ui-iconElement-icon' )
3897 .toggleClass( 'oo-ui-icon-' + this.icon
, !!this.icon
);
3898 if ( this.iconTitle
!== null ) {
3899 this.$icon
.attr( 'title', this.iconTitle
);
3906 * @param {Object|string|null} icon Symbolic icon name, or map of icon names keyed by language ID;
3907 * use the 'default' key to specify the icon to be used when there is no icon in the user's
3908 * language, use null to remove icon
3911 OO
.ui
.IconElement
.prototype.setIcon = function ( icon
) {
3912 icon
= OO
.isPlainObject( icon
) ? OO
.ui
.getLocalValue( icon
, null, 'default' ) : icon
;
3913 icon
= typeof icon
=== 'string' && icon
.trim().length
? icon
.trim() : null;
3915 if ( this.icon
!== icon
) {
3917 if ( this.icon
!== null ) {
3918 this.$icon
.removeClass( 'oo-ui-icon-' + this.icon
);
3920 if ( icon
!== null ) {
3921 this.$icon
.addClass( 'oo-ui-icon-' + icon
);
3927 this.$element
.toggleClass( 'oo-ui-iconElement', !!this.icon
);
3935 * @param {string|Function|null} icon Icon title text, a function that returns text or null
3939 OO
.ui
.IconElement
.prototype.setIconTitle = function ( iconTitle
) {
3940 iconTitle
= typeof iconTitle
=== 'function' ||
3941 ( typeof iconTitle
=== 'string' && iconTitle
.length
) ?
3942 OO
.ui
.resolveMsg( iconTitle
) : null;
3944 if ( this.iconTitle
!== iconTitle
) {
3945 this.iconTitle
= iconTitle
;
3947 if ( this.iconTitle
!== null ) {
3948 this.$icon
.attr( 'title', iconTitle
);
3950 this.$icon
.removeAttr( 'title' );
3961 * @return {string} Icon
3963 OO
.ui
.IconElement
.prototype.getIcon = function () {
3968 * Element containing an indicator.
3970 * Indicators are graphics, smaller than normal text. They can be used to describe unique status or
3971 * behavior. Indicators should only be used in exceptional cases; such as a button that opens a menu
3972 * instead of performing an action directly, or an item in a list which has errors that need to be
3979 * @param {Object} [config] Configuration options
3980 * @cfg {jQuery} [$indicator] Indicator node, assigned to #$indicator, omit to use a generated
3982 * @cfg {string} [indicator] Symbolic indicator name
3983 * @cfg {string} [indicatorTitle] Indicator title text or a function that returns text
3985 OO
.ui
.IndicatorElement
= function OoUiIndicatorElement( config
) {
3986 // Config intialization
3987 config
= config
|| {};
3990 this.$indicator
= null;
3991 this.indicator
= null;
3992 this.indicatorTitle
= null;
3995 this.setIndicator( config
.indicator
|| this.constructor.static.indicator
);
3996 this.setIndicatorTitle( config
.indicatorTitle
|| this.constructor.static.indicatorTitle
);
3997 this.setIndicatorElement( config
.$indicator
|| this.$( '<span>' ) );
4002 OO
.initClass( OO
.ui
.IndicatorElement
);
4004 /* Static Properties */
4011 * @property {string|null} Symbolic indicator name or null for no indicator
4013 OO
.ui
.IndicatorElement
.static.indicator
= null;
4020 * @property {string|Function|null} Indicator title text, a function that returns text or null for no
4023 OO
.ui
.IndicatorElement
.static.indicatorTitle
= null;
4028 * Set the indicator element.
4030 * If an element is already set, it will be cleaned up before setting up the new element.
4032 * @param {jQuery} $indicator Element to use as indicator
4034 OO
.ui
.IndicatorElement
.prototype.setIndicatorElement = function ( $indicator
) {
4035 if ( this.$indicator
) {
4037 .removeClass( 'oo-ui-indicatorElement-indicator oo-ui-indicator-' + this.indicator
)
4038 .removeAttr( 'title' );
4041 this.$indicator
= $indicator
4042 .addClass( 'oo-ui-indicatorElement-indicator' )
4043 .toggleClass( 'oo-ui-indicator-' + this.indicator
, !!this.indicator
);
4044 if ( this.indicatorTitle
!== null ) {
4045 this.$indicatorTitle
.attr( 'title', this.indicatorTitle
);
4052 * @param {string|null} indicator Symbolic name of indicator to use or null for no indicator
4055 OO
.ui
.IndicatorElement
.prototype.setIndicator = function ( indicator
) {
4056 indicator
= typeof indicator
=== 'string' && indicator
.length
? indicator
.trim() : null;
4058 if ( this.indicator
!== indicator
) {
4059 if ( this.$indicator
) {
4060 if ( this.indicator
!== null ) {
4061 this.$indicator
.removeClass( 'oo-ui-indicator-' + this.indicator
);
4063 if ( indicator
!== null ) {
4064 this.$indicator
.addClass( 'oo-ui-indicator-' + indicator
);
4067 this.indicator
= indicator
;
4070 this.$element
.toggleClass( 'oo-ui-indicatorElement', !!this.indicator
);
4076 * Set indicator title.
4078 * @param {string|Function|null} indicator Indicator title text, a function that returns text or
4079 * null for no indicator title
4082 OO
.ui
.IndicatorElement
.prototype.setIndicatorTitle = function ( indicatorTitle
) {
4083 indicatorTitle
= typeof indicatorTitle
=== 'function' ||
4084 ( typeof indicatorTitle
=== 'string' && indicatorTitle
.length
) ?
4085 OO
.ui
.resolveMsg( indicatorTitle
) : null;
4087 if ( this.indicatorTitle
!== indicatorTitle
) {
4088 this.indicatorTitle
= indicatorTitle
;
4089 if ( this.$indicator
) {
4090 if ( this.indicatorTitle
!== null ) {
4091 this.$indicator
.attr( 'title', indicatorTitle
);
4093 this.$indicator
.removeAttr( 'title' );
4104 * @return {string} title Symbolic name of indicator
4106 OO
.ui
.IndicatorElement
.prototype.getIndicator = function () {
4107 return this.indicator
;
4111 * Get indicator title.
4113 * @return {string} Indicator title text
4115 OO
.ui
.IndicatorElement
.prototype.getIndicatorTitle = function () {
4116 return this.indicatorTitle
;
4120 * Element containing a label.
4126 * @param {Object} [config] Configuration options
4127 * @cfg {jQuery} [$label] Label node, assigned to #$label, omit to use a generated `<span>`
4128 * @cfg {jQuery|string|Function} [label] Label nodes, text or a function that returns nodes or text
4129 * @cfg {boolean} [autoFitLabel=true] Whether to fit the label or not.
4131 OO
.ui
.LabelElement
= function OoUiLabelElement( config
) {
4132 // Config intialization
4133 config
= config
|| {};
4138 this.autoFitLabel
= config
.autoFitLabel
=== undefined || !!config
.autoFitLabel
;
4141 this.setLabel( config
.label
|| this.constructor.static.label
);
4142 this.setLabelElement( config
.$label
|| this.$( '<span>' ) );
4147 OO
.initClass( OO
.ui
.LabelElement
);
4149 /* Static Properties */
4156 * @property {string|Function|null} Label text; a function that returns nodes or text; or null for
4159 OO
.ui
.LabelElement
.static.label
= null;
4164 * Set the label element.
4166 * If an element is already set, it will be cleaned up before setting up the new element.
4168 * @param {jQuery} $label Element to use as label
4170 OO
.ui
.LabelElement
.prototype.setLabelElement = function ( $label
) {
4171 if ( this.$label
) {
4172 this.$label
.removeClass( 'oo-ui-labelElement-label' ).empty();
4175 this.$label
= $label
.addClass( 'oo-ui-labelElement-label' );
4176 this.setLabelContent( this.label
);
4182 * An empty string will result in the label being hidden. A string containing only whitespace will
4183 * be converted to a single
4185 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4186 * text; or null for no label
4189 OO
.ui
.LabelElement
.prototype.setLabel = function ( label
) {
4190 label
= typeof label
=== 'function' ? OO
.ui
.resolveMsg( label
) : label
;
4191 label
= ( typeof label
=== 'string' && label
.length
) || label
instanceof jQuery
? label
: null;
4193 if ( this.label
!== label
) {
4194 if ( this.$label
) {
4195 this.setLabelContent( label
);
4200 this.$element
.toggleClass( 'oo-ui-labelElement', !!this.label
);
4208 * @return {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4209 * text; or null for no label
4211 OO
.ui
.LabelElement
.prototype.getLabel = function () {
4220 OO
.ui
.LabelElement
.prototype.fitLabel = function () {
4221 if ( this.$label
&& this.$label
.autoEllipsis
&& this.autoFitLabel
) {
4222 this.$label
.autoEllipsis( { hasSpan
: false, tooltip
: true } );
4229 * Set the content of the label.
4231 * Do not call this method until after the label element has been set by #setLabelElement.
4234 * @param {jQuery|string|Function|null} label Label nodes; text; a function that returns nodes or
4235 * text; or null for no label
4237 OO
.ui
.LabelElement
.prototype.setLabelContent = function ( label
) {
4238 if ( typeof label
=== 'string' ) {
4239 if ( label
.match( /^\s*$/ ) ) {
4240 // Convert whitespace only string to a single non-breaking space
4241 this.$label
.html( ' ' );
4243 this.$label
.text( label
);
4245 } else if ( label
instanceof jQuery
) {
4246 this.$label
.empty().append( label
);
4248 this.$label
.empty();
4250 this.$label
.css( 'display', !label
? 'none' : '' );
4254 * Element containing an OO.ui.PopupWidget object.
4260 * @param {Object} [config] Configuration options
4261 * @cfg {Object} [popup] Configuration to pass to popup
4262 * @cfg {boolean} [autoClose=true] Popup auto-closes when it loses focus
4264 OO
.ui
.PopupElement
= function OoUiPopupElement( config
) {
4265 // Configuration initialization
4266 config
= config
|| {};
4269 this.popup
= new OO
.ui
.PopupWidget( $.extend(
4270 { autoClose
: true },
4272 { $: this.$, $autoCloseIgnore
: this.$element
}
4281 * @return {OO.ui.PopupWidget} Popup widget
4283 OO
.ui
.PopupElement
.prototype.getPopup = function () {
4288 * Element with named flags that can be added, removed, listed and checked.
4290 * A flag, when set, adds a CSS class on the `$element` by combining `oo-ui-flaggedElement-` with
4291 * the flag name. Flags are primarily useful for styling.
4297 * @param {Object} [config] Configuration options
4298 * @cfg {string[]} [flags=[]] Styling flags, e.g. 'primary', 'destructive' or 'constructive'
4299 * @cfg {jQuery} [$flagged] Flagged node, assigned to #$flagged, omit to use #$element
4301 OO
.ui
.FlaggedElement
= function OoUiFlaggedElement( config
) {
4302 // Config initialization
4303 config
= config
|| {};
4307 this.$flagged
= null;
4310 this.setFlags( config
.flags
);
4311 this.setFlaggedElement( config
.$flagged
|| this.$element
);
4318 * @param {Object.<string,boolean>} changes Object keyed by flag name containing boolean
4319 * added/removed properties
4325 * Set the flagged element.
4327 * If an element is already set, it will be cleaned up before setting up the new element.
4329 * @param {jQuery} $flagged Element to add flags to
4331 OO
.ui
.FlaggedElement
.prototype.setFlaggedElement = function ( $flagged
) {
4332 var classNames
= Object
.keys( this.flags
).map( function ( flag
) {
4333 return 'oo-ui-flaggedElement-' + flag
;
4336 if ( this.$flagged
) {
4337 this.$flagged
.removeClass( classNames
);
4340 this.$flagged
= $flagged
.addClass( classNames
);
4344 * Check if a flag is set.
4346 * @param {string} flag Name of flag
4347 * @return {boolean} Has flag
4349 OO
.ui
.FlaggedElement
.prototype.hasFlag = function ( flag
) {
4350 return flag
in this.flags
;
4354 * Get the names of all flags set.
4356 * @return {string[]} flags Flag names
4358 OO
.ui
.FlaggedElement
.prototype.getFlags = function () {
4359 return Object
.keys( this.flags
);
4368 OO
.ui
.FlaggedElement
.prototype.clearFlags = function () {
4369 var flag
, className
,
4372 classPrefix
= 'oo-ui-flaggedElement-';
4374 for ( flag
in this.flags
) {
4375 className
= classPrefix
+ flag
;
4376 changes
[flag
] = false;
4377 delete this.flags
[flag
];
4378 remove
.push( className
);
4381 if ( this.$flagged
) {
4382 this.$flagged
.removeClass( remove
.join( ' ' ) );
4385 this.emit( 'flag', changes
);
4391 * Add one or more flags.
4393 * @param {string|string[]|Object.<string, boolean>} flags One or more flags to add, or an object
4394 * keyed by flag name containing boolean set/remove instructions.
4398 OO
.ui
.FlaggedElement
.prototype.setFlags = function ( flags
) {
4399 var i
, len
, flag
, className
,
4403 classPrefix
= 'oo-ui-flaggedElement-';
4405 if ( typeof flags
=== 'string' ) {
4406 className
= classPrefix
+ flags
;
4408 if ( !this.flags
[flags
] ) {
4409 this.flags
[flags
] = true;
4410 add
.push( className
);
4412 } else if ( $.isArray( flags
) ) {
4413 for ( i
= 0, len
= flags
.length
; i
< len
; i
++ ) {
4415 className
= classPrefix
+ flag
;
4417 if ( !this.flags
[flag
] ) {
4418 changes
[flag
] = true;
4419 this.flags
[flag
] = true;
4420 add
.push( className
);
4423 } else if ( OO
.isPlainObject( flags
) ) {
4424 for ( flag
in flags
) {
4425 className
= classPrefix
+ flag
;
4426 if ( flags
[flag
] ) {
4428 if ( !this.flags
[flag
] ) {
4429 changes
[flag
] = true;
4430 this.flags
[flag
] = true;
4431 add
.push( className
);
4435 if ( this.flags
[flag
] ) {
4436 changes
[flag
] = false;
4437 delete this.flags
[flag
];
4438 remove
.push( className
);
4444 if ( this.$flagged
) {
4446 .addClass( add
.join( ' ' ) )
4447 .removeClass( remove
.join( ' ' ) );
4450 this.emit( 'flag', changes
);
4456 * Element with a title.
4458 * Titles are rendered by the browser and are made visible when hovering the element. Titles are
4459 * not visible on touch devices.
4465 * @param {Object} [config] Configuration options
4466 * @cfg {jQuery} [$titled] Titled node, assigned to #$titled, omit to use #$element
4467 * @cfg {string|Function} [title] Title text or a function that returns text
4469 OO
.ui
.TitledElement
= function OoUiTitledElement( config
) {
4470 // Config intialization
4471 config
= config
|| {};
4474 this.$titled
= null;
4478 this.setTitle( config
.title
|| this.constructor.static.title
);
4479 this.setTitledElement( config
.$titled
|| this.$element
);
4484 OO
.initClass( OO
.ui
.TitledElement
);
4486 /* Static Properties */
4493 * @property {string|Function} Title text or a function that returns text
4495 OO
.ui
.TitledElement
.static.title
= null;
4500 * Set the titled element.
4502 * If an element is already set, it will be cleaned up before setting up the new element.
4504 * @param {jQuery} $titled Element to set title on
4506 OO
.ui
.TitledElement
.prototype.setTitledElement = function ( $titled
) {
4507 if ( this.$titled
) {
4508 this.$titled
.removeAttr( 'title' );
4511 this.$titled
= $titled
;
4513 this.$titled
.attr( 'title', this.title
);
4520 * @param {string|Function|null} title Title text, a function that returns text or null for no title
4523 OO
.ui
.TitledElement
.prototype.setTitle = function ( title
) {
4524 title
= typeof title
=== 'string' ? OO
.ui
.resolveMsg( title
) : null;
4526 if ( this.title
!== title
) {
4527 if ( this.$titled
) {
4528 if ( title
!== null ) {
4529 this.$titled
.attr( 'title', title
);
4531 this.$titled
.removeAttr( 'title' );
4543 * @return {string} Title string
4545 OO
.ui
.TitledElement
.prototype.getTitle = function () {
4550 * Element that can be automatically clipped to visible boundaries.
4552 * Whenever the element's natural height changes, you have to call
4553 * #clip to make sure it's still clipping correctly.
4559 * @param {Object} [config] Configuration options
4560 * @cfg {jQuery} [$clippable] Nodes to clip, assigned to #$clippable, omit to use #$element
4562 OO
.ui
.ClippableElement
= function OoUiClippableElement( config
) {
4563 // Configuration initialization
4564 config
= config
|| {};
4567 this.$clippable
= null;
4568 this.clipping
= false;
4569 this.clippedHorizontally
= false;
4570 this.clippedVertically
= false;
4571 this.$clippableContainer
= null;
4572 this.$clippableScroller
= null;
4573 this.$clippableWindow
= null;
4574 this.idealWidth
= null;
4575 this.idealHeight
= null;
4576 this.onClippableContainerScrollHandler
= OO
.ui
.bind( this.clip
, this );
4577 this.onClippableWindowResizeHandler
= OO
.ui
.bind( this.clip
, this );
4580 this.setClippableElement( config
.$clippable
|| this.$element
);
4586 * Set clippable element.
4588 * If an element is already set, it will be cleaned up before setting up the new element.
4590 * @param {jQuery} $clippable Element to make clippable
4592 OO
.ui
.ClippableElement
.prototype.setClippableElement = function ( $clippable
) {
4593 if ( this.$clippable
) {
4594 this.$clippable
.removeClass( 'oo-ui-clippableElement-clippable' );
4595 this.$clippable
.css( { width
: '', height
: '' } );
4596 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4597 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4600 this.$clippable
= $clippable
.addClass( 'oo-ui-clippableElement-clippable' );
4607 * Do not turn clipping on until after the element is attached to the DOM and visible.
4609 * @param {boolean} [clipping] Enable clipping, omit to toggle
4612 OO
.ui
.ClippableElement
.prototype.toggleClipping = function ( clipping
) {
4613 clipping
= clipping
=== undefined ? !this.clipping
: !!clipping
;
4615 if ( this.clipping
!== clipping
) {
4616 this.clipping
= clipping
;
4618 this.$clippableContainer
= this.$( this.getClosestScrollableElementContainer() );
4619 // If the clippable container is the body, we have to listen to scroll events and check
4620 // jQuery.scrollTop on the window because of browser inconsistencies
4621 this.$clippableScroller
= this.$clippableContainer
.is( 'body' ) ?
4622 this.$( OO
.ui
.Element
.getWindow( this.$clippableContainer
) ) :
4623 this.$clippableContainer
;
4624 this.$clippableScroller
.on( 'scroll', this.onClippableContainerScrollHandler
);
4625 this.$clippableWindow
= this.$( this.getElementWindow() )
4626 .on( 'resize', this.onClippableWindowResizeHandler
);
4627 // Initial clip after visible
4630 this.$clippable
.css( { width
: '', height
: '' } );
4631 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4632 this.$clippable
.css( { overflowX
: '', overflowY
: '' } );
4634 this.$clippableContainer
= null;
4635 this.$clippableScroller
.off( 'scroll', this.onClippableContainerScrollHandler
);
4636 this.$clippableScroller
= null;
4637 this.$clippableWindow
.off( 'resize', this.onClippableWindowResizeHandler
);
4638 this.$clippableWindow
= null;
4646 * Check if the element will be clipped to fit the visible area of the nearest scrollable container.
4648 * @return {boolean} Element will be clipped to the visible area
4650 OO
.ui
.ClippableElement
.prototype.isClipping = function () {
4651 return this.clipping
;
4655 * Check if the bottom or right of the element is being clipped by the nearest scrollable container.
4657 * @return {boolean} Part of the element is being clipped
4659 OO
.ui
.ClippableElement
.prototype.isClipped = function () {
4660 return this.clippedHorizontally
|| this.clippedVertically
;
4664 * Check if the right of the element is being clipped by the nearest scrollable container.
4666 * @return {boolean} Part of the element is being clipped
4668 OO
.ui
.ClippableElement
.prototype.isClippedHorizontally = function () {
4669 return this.clippedHorizontally
;
4673 * Check if the bottom of the element is being clipped by the nearest scrollable container.
4675 * @return {boolean} Part of the element is being clipped
4677 OO
.ui
.ClippableElement
.prototype.isClippedVertically = function () {
4678 return this.clippedVertically
;
4682 * Set the ideal size.
4684 * @param {number|string} [width] Width as a number of pixels or CSS string with unit suffix
4685 * @param {number|string} [height] Height as a number of pixels or CSS string with unit suffix
4687 OO
.ui
.ClippableElement
.prototype.setIdealSize = function ( width
, height
) {
4688 this.idealWidth
= width
;
4689 this.idealHeight
= height
;
4693 * Clip element to visible boundaries and allow scrolling when needed. Call this method when
4694 * the element's natural height changes.
4696 * Element will be clipped the bottom or right of the element is within 10px of the edge of, or
4697 * overlapped by, the visible area of the nearest scrollable container.
4701 OO
.ui
.ClippableElement
.prototype.clip = function () {
4702 if ( !this.clipping
) {
4703 // this.$clippableContainer and this.$clippableWindow are null, so the below will fail
4708 cOffset
= this.$clippable
.offset(),
4709 $container
= this.$clippableContainer
.is( 'body' ) ?
4710 this.$clippableWindow
: this.$clippableContainer
,
4711 ccOffset
= $container
.offset() || { top
: 0, left
: 0 },
4712 ccHeight
= $container
.innerHeight() - buffer
,
4713 ccWidth
= $container
.innerWidth() - buffer
,
4714 scrollTop
= this.$clippableScroller
.scrollTop(),
4715 scrollLeft
= this.$clippableScroller
.scrollLeft(),
4716 desiredWidth
= ( ccOffset
.left
+ scrollLeft
+ ccWidth
) - cOffset
.left
,
4717 desiredHeight
= ( ccOffset
.top
+ scrollTop
+ ccHeight
) - cOffset
.top
,
4718 naturalWidth
= this.$clippable
.prop( 'scrollWidth' ),
4719 naturalHeight
= this.$clippable
.prop( 'scrollHeight' ),
4720 clipWidth
= desiredWidth
< naturalWidth
,
4721 clipHeight
= desiredHeight
< naturalHeight
;
4724 this.$clippable
.css( { overflowX
: 'auto', width
: desiredWidth
} );
4726 this.$clippable
.css( 'width', this.idealWidth
|| '' );
4727 this.$clippable
.width(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4728 this.$clippable
.css( 'overflowX', '' );
4731 this.$clippable
.css( { overflowY
: 'auto', height
: desiredHeight
} );
4733 this.$clippable
.css( 'height', this.idealHeight
|| '' );
4734 this.$clippable
.height(); // Force reflow for https://code.google.com/p/chromium/issues/detail?id=387290
4735 this.$clippable
.css( 'overflowY', '' );
4738 this.clippedHorizontally
= clipWidth
;
4739 this.clippedVertically
= clipHeight
;
4745 * Generic toolbar tool.
4749 * @extends OO.ui.Widget
4750 * @mixins OO.ui.IconElement
4753 * @param {OO.ui.ToolGroup} toolGroup
4754 * @param {Object} [config] Configuration options
4755 * @cfg {string|Function} [title] Title text or a function that returns text
4757 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
4758 // Config intialization
4759 config
= config
|| {};
4761 // Parent constructor
4762 OO
.ui
.Tool
.super.call( this, config
);
4764 // Mixin constructors
4765 OO
.ui
.IconElement
.call( this, config
);
4768 this.toolGroup
= toolGroup
;
4769 this.toolbar
= this.toolGroup
.getToolbar();
4770 this.active
= false;
4771 this.$title
= this.$( '<span>' );
4772 this.$link
= this.$( '<a>' );
4776 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
4779 this.$title
.addClass( 'oo-ui-tool-title' );
4781 .addClass( 'oo-ui-tool-link' )
4782 .append( this.$icon
, this.$title
)
4783 .prop( 'tabIndex', 0 )
4784 .attr( 'role', 'button' );
4786 .data( 'oo-ui-tool', this )
4788 'oo-ui-tool ' + 'oo-ui-tool-name-' +
4789 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
4791 .append( this.$link
);
4792 this.setTitle( config
.title
|| this.constructor.static.title
);
4797 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
4798 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.IconElement
);
4806 /* Static Properties */
4812 OO
.ui
.Tool
.static.tagName
= 'span';
4815 * Symbolic name of tool.
4820 * @property {string}
4822 OO
.ui
.Tool
.static.name
= '';
4830 * @property {string}
4832 OO
.ui
.Tool
.static.group
= '';
4837 * Title is used as a tooltip when the tool is part of a bar tool group, or a label when the tool
4838 * is part of a list or menu tool group. If a trigger is associated with an action by the same name
4839 * as the tool, a description of its keyboard shortcut for the appropriate platform will be
4840 * appended to the title if the tool is part of a bar tool group.
4845 * @property {string|Function} Title text or a function that returns text
4847 OO
.ui
.Tool
.static.title
= '';
4850 * Tool can be automatically added to catch-all groups.
4854 * @property {boolean}
4856 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
4859 * Tool can be automatically added to named groups.
4862 * @property {boolean}
4865 OO
.ui
.Tool
.static.autoAddToGroup
= true;
4868 * Check if this tool is compatible with given data.
4872 * @param {Mixed} data Data to check
4873 * @return {boolean} Tool can be used with data
4875 OO
.ui
.Tool
.static.isCompatibleWith = function () {
4882 * Handle the toolbar state being updated.
4884 * This is an abstract method that must be overridden in a concrete subclass.
4888 OO
.ui
.Tool
.prototype.onUpdateState = function () {
4890 'OO.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
4895 * Handle the tool being selected.
4897 * This is an abstract method that must be overridden in a concrete subclass.
4901 OO
.ui
.Tool
.prototype.onSelect = function () {
4903 'OO.ui.Tool.onSelect not implemented in this subclass:' + this.constructor
4908 * Check if the button is active.
4910 * @param {boolean} Button is active
4912 OO
.ui
.Tool
.prototype.isActive = function () {
4917 * Make the button appear active or inactive.
4919 * @param {boolean} state Make button appear active
4921 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
4922 this.active
= !!state
;
4923 if ( this.active
) {
4924 this.$element
.addClass( 'oo-ui-tool-active' );
4926 this.$element
.removeClass( 'oo-ui-tool-active' );
4931 * Get the tool title.
4933 * @param {string|Function} title Title text or a function that returns text
4936 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
4937 this.title
= OO
.ui
.resolveMsg( title
);
4943 * Get the tool title.
4945 * @return {string} Title text
4947 OO
.ui
.Tool
.prototype.getTitle = function () {
4952 * Get the tool's symbolic name.
4954 * @return {string} Symbolic name of tool
4956 OO
.ui
.Tool
.prototype.getName = function () {
4957 return this.constructor.static.name
;
4963 OO
.ui
.Tool
.prototype.updateTitle = function () {
4964 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
4965 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
4966 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
4973 .addClass( 'oo-ui-tool-accel' )
4977 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
4978 tooltipParts
.push( this.title
);
4980 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
4981 tooltipParts
.push( accel
);
4983 if ( tooltipParts
.length
) {
4984 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
4986 this.$link
.removeAttr( 'title' );
4993 OO
.ui
.Tool
.prototype.destroy = function () {
4994 this.toolbar
.disconnect( this );
4995 this.$element
.remove();
4999 * Collection of tool groups.
5002 * @extends OO.ui.Element
5003 * @mixins OO.EventEmitter
5004 * @mixins OO.ui.GroupElement
5007 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
5008 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating tool groups
5009 * @param {Object} [config] Configuration options
5010 * @cfg {boolean} [actions] Add an actions section opposite to the tools
5011 * @cfg {boolean} [shadow] Add a shadow below the toolbar
5013 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
5014 // Configuration initialization
5015 config
= config
|| {};
5017 // Parent constructor
5018 OO
.ui
.Toolbar
.super.call( this, config
);
5020 // Mixin constructors
5021 OO
.EventEmitter
.call( this );
5022 OO
.ui
.GroupElement
.call( this, config
);
5025 this.toolFactory
= toolFactory
;
5026 this.toolGroupFactory
= toolGroupFactory
;
5029 this.$bar
= this.$( '<div>' );
5030 this.$actions
= this.$( '<div>' );
5031 this.initialized
= false;
5035 .add( this.$bar
).add( this.$group
).add( this.$actions
)
5036 .on( 'mousedown touchstart', OO
.ui
.bind( this.onPointerDown
, this ) );
5039 this.$group
.addClass( 'oo-ui-toolbar-tools' );
5040 this.$bar
.addClass( 'oo-ui-toolbar-bar' ).append( this.$group
);
5041 if ( config
.actions
) {
5042 this.$actions
.addClass( 'oo-ui-toolbar-actions' );
5043 this.$bar
.append( this.$actions
);
5045 this.$bar
.append( '<div style="clear:both"></div>' );
5046 if ( config
.shadow
) {
5047 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
5049 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
5054 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
5055 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
5056 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.GroupElement
);
5061 * Get the tool factory.
5063 * @return {OO.ui.ToolFactory} Tool factory
5065 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
5066 return this.toolFactory
;
5070 * Get the tool group factory.
5072 * @return {OO.Factory} Tool group factory
5074 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
5075 return this.toolGroupFactory
;
5079 * Handles mouse down events.
5081 * @param {jQuery.Event} e Mouse down event
5083 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
5084 var $closestWidgetToEvent
= this.$( e
.target
).closest( '.oo-ui-widget' ),
5085 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
5086 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[0] === $closestWidgetToToolbar
[0] ) {
5092 * Sets up handles and preloads required information for the toolbar to work.
5093 * This must be called immediately after it is attached to a visible document.
5095 OO
.ui
.Toolbar
.prototype.initialize = function () {
5096 this.initialized
= true;
5102 * Tools can be specified in the following ways:
5104 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5105 * - All tools in a group: `{ group: 'group-name' }`
5106 * - All tools: `'*'` - Using this will make the group a list with a "More" label by default
5108 * @param {Object.<string,Array>} groups List of tool group configurations
5109 * @param {Array|string} [groups.include] Tools to include
5110 * @param {Array|string} [groups.exclude] Tools to exclude
5111 * @param {Array|string} [groups.promote] Tools to promote to the beginning
5112 * @param {Array|string} [groups.demote] Tools to demote to the end
5114 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
5115 var i
, len
, type
, group
,
5117 defaultType
= 'bar';
5119 // Cleanup previous groups
5122 // Build out new groups
5123 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
5125 if ( group
.include
=== '*' ) {
5126 // Apply defaults to catch-all groups
5127 if ( group
.type
=== undefined ) {
5128 group
.type
= 'list';
5130 if ( group
.label
=== undefined ) {
5131 group
.label
= 'ooui-toolbar-more';
5134 // Check type has been registered
5135 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
5137 this.getToolGroupFactory().create( type
, this, $.extend( { $: this.$ }, group
) )
5140 this.addItems( items
);
5144 * Remove all tools and groups from the toolbar.
5146 OO
.ui
.Toolbar
.prototype.reset = function () {
5151 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
5152 this.items
[i
].destroy();
5158 * Destroys toolbar, removing event handlers and DOM elements.
5160 * Call this whenever you are done using a toolbar.
5162 OO
.ui
.Toolbar
.prototype.destroy = function () {
5164 this.$element
.remove();
5168 * Check if tool has not been used yet.
5170 * @param {string} name Symbolic name of tool
5171 * @return {boolean} Tool is available
5173 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
5174 return !this.tools
[name
];
5178 * Prevent tool from being used again.
5180 * @param {OO.ui.Tool} tool Tool to reserve
5182 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
5183 this.tools
[tool
.getName()] = tool
;
5187 * Allow tool to be used again.
5189 * @param {OO.ui.Tool} tool Tool to release
5191 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
5192 delete this.tools
[tool
.getName()];
5196 * Get accelerator label for tool.
5198 * This is a stub that should be overridden to provide access to accelerator information.
5200 * @param {string} name Symbolic name of tool
5201 * @return {string|undefined} Tool accelerator label if available
5203 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
5208 * Collection of tools.
5210 * Tools can be specified in the following ways:
5212 * - A specific tool: `{ name: 'tool-name' }` or `'tool-name'`
5213 * - All tools in a group: `{ group: 'group-name' }`
5214 * - All tools: `'*'`
5218 * @extends OO.ui.Widget
5219 * @mixins OO.ui.GroupElement
5222 * @param {OO.ui.Toolbar} toolbar
5223 * @param {Object} [config] Configuration options
5224 * @cfg {Array|string} [include=[]] List of tools to include
5225 * @cfg {Array|string} [exclude=[]] List of tools to exclude
5226 * @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
5227 * @cfg {Array|string} [demote=[]] List of tools to demote to the end
5229 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
5230 // Configuration initialization
5231 config
= config
|| {};
5233 // Parent constructor
5234 OO
.ui
.ToolGroup
.super.call( this, config
);
5236 // Mixin constructors
5237 OO
.ui
.GroupElement
.call( this, config
);
5240 this.toolbar
= toolbar
;
5242 this.pressed
= null;
5243 this.autoDisabled
= false;
5244 this.include
= config
.include
|| [];
5245 this.exclude
= config
.exclude
|| [];
5246 this.promote
= config
.promote
|| [];
5247 this.demote
= config
.demote
|| [];
5248 this.onCapturedMouseUpHandler
= OO
.ui
.bind( this.onCapturedMouseUp
, this );
5252 'mousedown touchstart': OO
.ui
.bind( this.onPointerDown
, this ),
5253 'mouseup touchend': OO
.ui
.bind( this.onPointerUp
, this ),
5254 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
5255 mouseout
: OO
.ui
.bind( this.onMouseOut
, this )
5257 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
5258 this.aggregate( { disable
: 'itemDisable' } );
5259 this.connect( this, { itemDisable
: 'updateDisabled' } );
5262 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
5264 .addClass( 'oo-ui-toolGroup' )
5265 .append( this.$group
);
5271 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
5272 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.GroupElement
);
5280 /* Static Properties */
5283 * Show labels in tooltips.
5287 * @property {boolean}
5289 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
5292 * Show acceleration labels in tooltips.
5296 * @property {boolean}
5298 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
5301 * Automatically disable the toolgroup when all tools are disabled
5305 * @property {boolean}
5307 OO
.ui
.ToolGroup
.static.autoDisable
= true;
5314 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
5315 return this.autoDisabled
|| OO
.ui
.ToolGroup
.super.prototype.isDisabled
.apply( this, arguments
);
5321 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
5322 var i
, item
, allDisabled
= true;
5324 if ( this.constructor.static.autoDisable
) {
5325 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
5326 item
= this.items
[i
];
5327 if ( !item
.isDisabled() ) {
5328 allDisabled
= false;
5332 this.autoDisabled
= allDisabled
;
5334 OO
.ui
.ToolGroup
.super.prototype.updateDisabled
.apply( this, arguments
);
5338 * Handle mouse down events.
5340 * @param {jQuery.Event} e Mouse down event
5342 OO
.ui
.ToolGroup
.prototype.onPointerDown = function ( e
) {
5343 // e.which is 0 for touch events, 1 for left mouse button
5344 if ( !this.isDisabled() && e
.which
<= 1 ) {
5345 this.pressed
= this.getTargetTool( e
);
5346 if ( this.pressed
) {
5347 this.pressed
.setActive( true );
5348 this.getElementDocument().addEventListener(
5349 'mouseup', this.onCapturedMouseUpHandler
, true
5357 * Handle captured mouse up events.
5359 * @param {Event} e Mouse up event
5361 OO
.ui
.ToolGroup
.prototype.onCapturedMouseUp = function ( e
) {
5362 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler
, true );
5363 // onPointerUp may be called a second time, depending on where the mouse is when the button is
5364 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
5365 this.onPointerUp( e
);
5369 * Handle mouse up events.
5371 * @param {jQuery.Event} e Mouse up event
5373 OO
.ui
.ToolGroup
.prototype.onPointerUp = function ( e
) {
5374 var tool
= this.getTargetTool( e
);
5376 // e.which is 0 for touch events, 1 for left mouse button
5377 if ( !this.isDisabled() && e
.which
<= 1 && this.pressed
&& this.pressed
=== tool
) {
5378 this.pressed
.onSelect();
5381 this.pressed
= null;
5386 * Handle mouse over events.
5388 * @param {jQuery.Event} e Mouse over event
5390 OO
.ui
.ToolGroup
.prototype.onMouseOver = function ( e
) {
5391 var tool
= this.getTargetTool( e
);
5393 if ( this.pressed
&& this.pressed
=== tool
) {
5394 this.pressed
.setActive( true );
5399 * Handle mouse out events.
5401 * @param {jQuery.Event} e Mouse out event
5403 OO
.ui
.ToolGroup
.prototype.onMouseOut = function ( e
) {
5404 var tool
= this.getTargetTool( e
);
5406 if ( this.pressed
&& this.pressed
=== tool
) {
5407 this.pressed
.setActive( false );
5412 * Get the closest tool to a jQuery.Event.
5414 * Only tool links are considered, which prevents other elements in the tool such as popups from
5415 * triggering tool group interactions.
5418 * @param {jQuery.Event} e
5419 * @return {OO.ui.Tool|null} Tool, `null` if none was found
5421 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
5423 $item
= this.$( e
.target
).closest( '.oo-ui-tool-link' );
5425 if ( $item
.length
) {
5426 tool
= $item
.parent().data( 'oo-ui-tool' );
5429 return tool
&& !tool
.isDisabled() ? tool
: null;
5433 * Handle tool registry register events.
5435 * If a tool is registered after the group is created, we must repopulate the list to account for:
5437 * - a tool being added that may be included
5438 * - a tool already included being overridden
5440 * @param {string} name Symbolic name of tool
5442 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
5447 * Get the toolbar this group is in.
5449 * @return {OO.ui.Toolbar} Toolbar of group
5451 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
5452 return this.toolbar
;
5456 * Add and remove tools based on configuration.
5458 OO
.ui
.ToolGroup
.prototype.populate = function () {
5459 var i
, len
, name
, tool
,
5460 toolFactory
= this.toolbar
.getToolFactory(),
5464 list
= this.toolbar
.getToolFactory().getTools(
5465 this.include
, this.exclude
, this.promote
, this.demote
5468 // Build a list of needed tools
5469 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
5473 toolFactory
.lookup( name
) &&
5474 // Tool is available or is already in this group
5475 ( this.toolbar
.isToolAvailable( name
) || this.tools
[name
] )
5477 tool
= this.tools
[name
];
5479 // Auto-initialize tools on first use
5480 this.tools
[name
] = tool
= toolFactory
.create( name
, this );
5483 this.toolbar
.reserveTool( tool
);
5488 // Remove tools that are no longer needed
5489 for ( name
in this.tools
) {
5490 if ( !names
[name
] ) {
5491 this.tools
[name
].destroy();
5492 this.toolbar
.releaseTool( this.tools
[name
] );
5493 remove
.push( this.tools
[name
] );
5494 delete this.tools
[name
];
5497 if ( remove
.length
) {
5498 this.removeItems( remove
);
5500 // Update emptiness state
5502 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
5504 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
5506 // Re-add tools (moving existing ones to new locations)
5507 this.addItems( add
);
5508 // Disabled state may depend on items
5509 this.updateDisabled();
5513 * Destroy tool group.
5515 OO
.ui
.ToolGroup
.prototype.destroy = function () {
5519 this.toolbar
.getToolFactory().disconnect( this );
5520 for ( name
in this.tools
) {
5521 this.toolbar
.releaseTool( this.tools
[name
] );
5522 this.tools
[name
].disconnect( this ).destroy();
5523 delete this.tools
[name
];
5525 this.$element
.remove();
5529 * Dialog for showing a message.
5532 * - Registers two actions by default (safe and primary).
5533 * - Renders action widgets in the footer.
5536 * @extends OO.ui.Dialog
5539 * @param {Object} [config] Configuration options
5541 OO
.ui
.MessageDialog
= function OoUiMessageDialog( config
) {
5542 // Parent constructor
5543 OO
.ui
.MessageDialog
.super.call( this, config
);
5546 this.verticalActionLayout
= null;
5549 this.$element
.addClass( 'oo-ui-messageDialog' );
5554 OO
.inheritClass( OO
.ui
.MessageDialog
, OO
.ui
.Dialog
);
5556 /* Static Properties */
5558 OO
.ui
.MessageDialog
.static.name
= 'message';
5560 OO
.ui
.MessageDialog
.static.size
= 'small';
5562 OO
.ui
.MessageDialog
.static.verbose
= false;
5567 * A confirmation dialog's title should describe what the progressive action will do. An alert
5568 * dialog's title should describe what event occured.
5572 * @property {jQuery|string|Function|null}
5574 OO
.ui
.MessageDialog
.static.title
= null;
5577 * A confirmation dialog's message should describe the consequences of the progressive action. An
5578 * alert dialog's message should describe why the event occured.
5582 * @property {jQuery|string|Function|null}
5584 OO
.ui
.MessageDialog
.static.message
= null;
5586 OO
.ui
.MessageDialog
.static.actions
= [
5587 { action
: 'accept', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-accept' ), flags
: 'primary' },
5588 { action
: 'reject', label
: OO
.ui
.deferMsg( 'ooui-dialog-message-reject' ), flags
: 'safe' }
5596 OO
.ui
.MessageDialog
.prototype.onActionResize = function ( action
) {
5598 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5602 * Toggle action layout between vertical and horizontal.
5604 * @param {boolean} [value] Layout actions vertically, omit to toggle
5607 OO
.ui
.MessageDialog
.prototype.toggleVerticalActionLayout = function ( value
) {
5608 value
= value
=== undefined ? !this.verticalActionLayout
: !!value
;
5610 if ( value
!== this.verticalActionLayout
) {
5611 this.verticalActionLayout
= value
;
5613 .toggleClass( 'oo-ui-messageDialog-actions-vertical', value
)
5614 .toggleClass( 'oo-ui-messageDialog-actions-horizontal', !value
);
5623 OO
.ui
.MessageDialog
.prototype.getActionProcess = function ( action
) {
5625 return new OO
.ui
.Process( function () {
5626 this.close( { action
: action
} );
5629 return OO
.ui
.MessageDialog
.super.prototype.getActionProcess
.call( this, action
);
5635 * @param {Object} [data] Dialog opening data
5636 * @param {jQuery|string|Function|null} [data.title] Description of the action being confirmed
5637 * @param {jQuery|string|Function|null} [data.message] Description of the action's consequence
5638 * @param {boolean} [data.verbose] Message is verbose and should be styled as a long message
5639 * @param {Object[]} [data.actions] List of OO.ui.ActionOptionWidget configuration options for each
5642 OO
.ui
.MessageDialog
.prototype.getSetupProcess = function ( data
) {
5646 return OO
.ui
.MessageDialog
.super.prototype.getSetupProcess
.call( this, data
)
5647 .next( function () {
5648 this.title
.setLabel(
5649 data
.title
!== undefined ? data
.title
: this.constructor.static.title
5651 this.message
.setLabel(
5652 data
.message
!== undefined ? data
.message
: this.constructor.static.message
5654 this.message
.$element
.toggleClass(
5655 'oo-ui-messageDialog-message-verbose',
5656 data
.verbose
!== undefined ? data
.verbose
: this.constructor.static.verbose
5664 OO
.ui
.MessageDialog
.prototype.getBodyHeight = function () {
5665 return Math
.round( this.text
.$element
.outerHeight( true ) );
5671 OO
.ui
.MessageDialog
.prototype.initialize = function () {
5673 OO
.ui
.MessageDialog
.super.prototype.initialize
.call( this );
5676 this.$actions
= this.$( '<div>' );
5677 this.container
= new OO
.ui
.PanelLayout( {
5678 $: this.$, scrollable
: true, classes
: [ 'oo-ui-messageDialog-container' ]
5680 this.text
= new OO
.ui
.PanelLayout( {
5681 $: this.$, padded
: true, expanded
: false, classes
: [ 'oo-ui-messageDialog-text' ]
5683 this.message
= new OO
.ui
.LabelWidget( {
5684 $: this.$, classes
: [ 'oo-ui-messageDialog-message' ]
5688 this.title
.$element
.addClass( 'oo-ui-messageDialog-title' );
5689 this.$content
.addClass( 'oo-ui-messageDialog-content' );
5690 this.container
.$element
.append( this.text
.$element
);
5691 this.text
.$element
.append( this.title
.$element
, this.message
.$element
);
5692 this.$body
.append( this.container
.$element
);
5693 this.$actions
.addClass( 'oo-ui-messageDialog-actions' );
5694 this.$foot
.append( this.$actions
);
5700 OO
.ui
.MessageDialog
.prototype.attachActions = function () {
5701 var i
, len
, other
, special
, others
;
5704 OO
.ui
.MessageDialog
.super.prototype.attachActions
.call( this );
5706 special
= this.actions
.getSpecial();
5707 others
= this.actions
.getOthers();
5708 if ( special
.safe
) {
5709 this.$actions
.append( special
.safe
.$element
);
5710 special
.safe
.toggleFramed( false );
5712 if ( others
.length
) {
5713 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5715 this.$actions
.append( other
.$element
);
5716 other
.toggleFramed( false );
5719 if ( special
.primary
) {
5720 this.$actions
.append( special
.primary
.$element
);
5721 special
.primary
.toggleFramed( false );
5725 if ( !this.isOpening() ) {
5726 this.manager
.updateWindowSize( this );
5728 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5732 * Fit action actions into columns or rows.
5734 * Columns will be used if all labels can fit without overflow, otherwise rows will be used.
5736 OO
.ui
.MessageDialog
.prototype.fitActions = function () {
5738 actions
= this.actions
.get();
5741 this.toggleVerticalActionLayout( false );
5742 for ( i
= 0, len
= actions
.length
; i
< len
; i
++ ) {
5743 action
= actions
[i
];
5744 if ( action
.$element
.innerWidth() < action
.$label
.outerWidth( true ) ) {
5745 this.toggleVerticalActionLayout( true );
5752 * Navigation dialog window.
5755 * - Show and hide errors.
5756 * - Retry an action.
5759 * - Renders header with dialog title and one action widget on either side
5760 * (a 'safe' button on the left, and a 'primary' button on the right, both of
5761 * which close the dialog).
5762 * - Displays any action widgets in the footer (none by default).
5763 * - Ability to dismiss errors.
5765 * Subclass responsibilities:
5766 * - Register a 'safe' action.
5767 * - Register a 'primary' action.
5768 * - Add content to the dialog.
5772 * @extends OO.ui.Dialog
5775 * @param {Object} [config] Configuration options
5777 OO
.ui
.ProcessDialog
= function OoUiProcessDialog( config
) {
5778 // Parent constructor
5779 OO
.ui
.ProcessDialog
.super.call( this, config
);
5782 this.$element
.addClass( 'oo-ui-processDialog' );
5787 OO
.inheritClass( OO
.ui
.ProcessDialog
, OO
.ui
.Dialog
);
5792 * Handle dismiss button click events.
5796 OO
.ui
.ProcessDialog
.prototype.onDismissErrorButtonClick = function () {
5801 * Handle retry button click events.
5803 * Hides errors and then tries again.
5805 OO
.ui
.ProcessDialog
.prototype.onRetryButtonClick = function () {
5807 this.executeAction( this.currentAction
.getAction() );
5813 OO
.ui
.ProcessDialog
.prototype.onActionResize = function ( action
) {
5814 if ( this.actions
.isSpecial( action
) ) {
5817 return OO
.ui
.ProcessDialog
.super.prototype.onActionResize
.call( this, action
);
5823 OO
.ui
.ProcessDialog
.prototype.initialize = function () {
5825 OO
.ui
.ProcessDialog
.super.prototype.initialize
.call( this );
5828 this.$navigation
= this.$( '<div>' );
5829 this.$location
= this.$( '<div>' );
5830 this.$safeActions
= this.$( '<div>' );
5831 this.$primaryActions
= this.$( '<div>' );
5832 this.$otherActions
= this.$( '<div>' );
5833 this.dismissButton
= new OO
.ui
.ButtonWidget( {
5835 label
: OO
.ui
.msg( 'ooui-dialog-process-dismiss' )
5837 this.retryButton
= new OO
.ui
.ButtonWidget( {
5839 label
: OO
.ui
.msg( 'ooui-dialog-process-retry' )
5841 this.$errors
= this.$( '<div>' );
5842 this.$errorsTitle
= this.$( '<div>' );
5845 this.dismissButton
.connect( this, { click
: 'onDismissErrorButtonClick' } );
5846 this.retryButton
.connect( this, { click
: 'onRetryButtonClick' } );
5849 this.title
.$element
.addClass( 'oo-ui-processDialog-title' );
5851 .append( this.title
.$element
)
5852 .addClass( 'oo-ui-processDialog-location' );
5853 this.$safeActions
.addClass( 'oo-ui-processDialog-actions-safe' );
5854 this.$primaryActions
.addClass( 'oo-ui-processDialog-actions-primary' );
5855 this.$otherActions
.addClass( 'oo-ui-processDialog-actions-other' );
5857 .addClass( 'oo-ui-processDialog-errors-title' )
5858 .text( OO
.ui
.msg( 'ooui-dialog-process-error' ) );
5860 .addClass( 'oo-ui-processDialog-errors' )
5861 .append( this.$errorsTitle
, this.dismissButton
.$element
, this.retryButton
.$element
);
5863 .addClass( 'oo-ui-processDialog-content' )
5864 .append( this.$errors
);
5866 .addClass( 'oo-ui-processDialog-navigation' )
5867 .append( this.$safeActions
, this.$location
, this.$primaryActions
);
5868 this.$head
.append( this.$navigation
);
5869 this.$foot
.append( this.$otherActions
);
5875 OO
.ui
.ProcessDialog
.prototype.attachActions = function () {
5876 var i
, len
, other
, special
, others
;
5879 OO
.ui
.ProcessDialog
.super.prototype.attachActions
.call( this );
5881 special
= this.actions
.getSpecial();
5882 others
= this.actions
.getOthers();
5883 if ( special
.primary
) {
5884 this.$primaryActions
.append( special
.primary
.$element
);
5885 special
.primary
.toggleFramed( true );
5887 if ( others
.length
) {
5888 for ( i
= 0, len
= others
.length
; i
< len
; i
++ ) {
5890 this.$otherActions
.append( other
.$element
);
5891 other
.toggleFramed( true );
5894 if ( special
.safe
) {
5895 this.$safeActions
.append( special
.safe
.$element
);
5896 special
.safe
.toggleFramed( true );
5900 this.$body
.css( 'bottom', this.$foot
.outerHeight( true ) );
5906 OO
.ui
.ProcessDialog
.prototype.executeAction = function ( action
) {
5907 OO
.ui
.ProcessDialog
.super.prototype.executeAction
.call( this, action
)
5908 .fail( OO
.ui
.bind( this.showErrors
, this ) );
5912 * Fit label between actions.
5916 OO
.ui
.ProcessDialog
.prototype.fitLabel = function () {
5917 var width
= Math
.max(
5918 this.$safeActions
.is( ':visible' ) ? this.$safeActions
.width() : 0,
5919 this.$primaryActions
.is( ':visible' ) ? this.$primaryActions
.width() : 0
5921 this.$location
.css( { paddingLeft
: width
, paddingRight
: width
} );
5927 * Handle errors that occured durring accept or reject processes.
5929 * @param {OO.ui.Error[]} errors Errors to be handled
5931 OO
.ui
.ProcessDialog
.prototype.showErrors = function ( errors
) {
5936 for ( i
= 0, len
= errors
.length
; i
< len
; i
++ ) {
5937 if ( !errors
[i
].isRecoverable() ) {
5938 recoverable
= false;
5940 $item
= this.$( '<div>' )
5941 .addClass( 'oo-ui-processDialog-error' )
5942 .append( errors
[i
].getMessage() );
5943 items
.push( $item
[0] );
5945 this.$errorItems
= this.$( items
);
5946 if ( recoverable
) {
5947 this.retryButton
.clearFlags().setFlags( this.currentAction
.getFlags() );
5949 this.currentAction
.setDisabled( true );
5951 this.retryButton
.toggle( recoverable
);
5952 this.$errorsTitle
.after( this.$errorItems
);
5953 this.$errors
.show().scrollTop( 0 );
5959 OO
.ui
.ProcessDialog
.prototype.hideErrors = function () {
5960 this.$errors
.hide();
5961 this.$errorItems
.remove();
5962 this.$errorItems
= null;
5966 * Layout containing a series of pages.
5969 * @extends OO.ui.Layout
5972 * @param {Object} [config] Configuration options
5973 * @cfg {boolean} [continuous=false] Show all pages, one after another
5974 * @cfg {boolean} [autoFocus=true] Focus on the first focusable element when changing to a page
5975 * @cfg {boolean} [outlined=false] Show an outline
5976 * @cfg {boolean} [editable=false] Show controls for adding, removing and reordering pages
5978 OO
.ui
.BookletLayout
= function OoUiBookletLayout( config
) {
5979 // Initialize configuration
5980 config
= config
|| {};
5982 // Parent constructor
5983 OO
.ui
.BookletLayout
.super.call( this, config
);
5986 this.currentPageName
= null;
5988 this.ignoreFocus
= false;
5989 this.stackLayout
= new OO
.ui
.StackLayout( { $: this.$, continuous
: !!config
.continuous
} );
5990 this.autoFocus
= config
.autoFocus
=== undefined || !!config
.autoFocus
;
5991 this.outlineVisible
= false;
5992 this.outlined
= !!config
.outlined
;
5993 if ( this.outlined
) {
5994 this.editable
= !!config
.editable
;
5995 this.outlineControlsWidget
= null;
5996 this.outlineWidget
= new OO
.ui
.OutlineWidget( { $: this.$ } );
5997 this.outlinePanel
= new OO
.ui
.PanelLayout( { $: this.$, scrollable
: true } );
5998 this.gridLayout
= new OO
.ui
.GridLayout(
5999 [ this.outlinePanel
, this.stackLayout
],
6000 { $: this.$, widths
: [ 1, 2 ] }
6002 this.outlineVisible
= true;
6003 if ( this.editable
) {
6004 this.outlineControlsWidget
= new OO
.ui
.OutlineControlsWidget(
6005 this.outlineWidget
, { $: this.$ }
6011 this.stackLayout
.connect( this, { set: 'onStackLayoutSet' } );
6012 if ( this.outlined
) {
6013 this.outlineWidget
.connect( this, { select
: 'onOutlineWidgetSelect' } );
6015 if ( this.autoFocus
) {
6016 // Event 'focus' does not bubble, but 'focusin' does
6017 this.stackLayout
.onDOMEvent( 'focusin', OO
.ui
.bind( this.onStackLayoutFocus
, this ) );
6021 this.$element
.addClass( 'oo-ui-bookletLayout' );
6022 this.stackLayout
.$element
.addClass( 'oo-ui-bookletLayout-stackLayout' );
6023 if ( this.outlined
) {
6024 this.outlinePanel
.$element
6025 .addClass( 'oo-ui-bookletLayout-outlinePanel' )
6026 .append( this.outlineWidget
.$element
);
6027 if ( this.editable
) {
6028 this.outlinePanel
.$element
6029 .addClass( 'oo-ui-bookletLayout-outlinePanel-editable' )
6030 .append( this.outlineControlsWidget
.$element
);
6032 this.$element
.append( this.gridLayout
.$element
);
6034 this.$element
.append( this.stackLayout
.$element
);
6040 OO
.inheritClass( OO
.ui
.BookletLayout
, OO
.ui
.Layout
);
6046 * @param {OO.ui.PageLayout} page Current page
6051 * @param {OO.ui.PageLayout[]} page Added pages
6052 * @param {number} index Index pages were added at
6057 * @param {OO.ui.PageLayout[]} pages Removed pages
6063 * Handle stack layout focus.
6065 * @param {jQuery.Event} e Focusin event
6067 OO
.ui
.BookletLayout
.prototype.onStackLayoutFocus = function ( e
) {
6070 // Find the page that an element was focused within
6071 $target
= $( e
.target
).closest( '.oo-ui-pageLayout' );
6072 for ( name
in this.pages
) {
6073 // Check for page match, exclude current page to find only page changes
6074 if ( this.pages
[name
].$element
[0] === $target
[0] && name
!== this.currentPageName
) {
6075 this.setPage( name
);
6082 * Handle stack layout set events.
6084 * @param {OO.ui.PanelLayout|null} page The page panel that is now the current panel
6086 OO
.ui
.BookletLayout
.prototype.onStackLayoutSet = function ( page
) {
6087 var $input
, layout
= this;
6089 page
.scrollElementIntoView( { complete: function () {
6090 if ( layout
.autoFocus
) {
6091 // Set focus to the first input if nothing on the page is focused yet
6092 if ( !page
.$element
.find( ':focus' ).length
) {
6093 $input
= page
.$element
.find( ':input:first' );
6094 if ( $input
.length
) {
6104 * Handle outline widget select events.
6106 * @param {OO.ui.OptionWidget|null} item Selected item
6108 OO
.ui
.BookletLayout
.prototype.onOutlineWidgetSelect = function ( item
) {
6110 this.setPage( item
.getData() );
6115 * Check if booklet has an outline.
6119 OO
.ui
.BookletLayout
.prototype.isOutlined = function () {
6120 return this.outlined
;
6124 * Check if booklet has editing controls.
6128 OO
.ui
.BookletLayout
.prototype.isEditable = function () {
6129 return this.editable
;
6133 * Check if booklet has a visible outline.
6137 OO
.ui
.BookletLayout
.prototype.isOutlineVisible = function () {
6138 return this.outlined
&& this.outlineVisible
;
6142 * Hide or show the outline.
6144 * @param {boolean} [show] Show outline, omit to invert current state
6147 OO
.ui
.BookletLayout
.prototype.toggleOutline = function ( show
) {
6148 if ( this.outlined
) {
6149 show
= show
=== undefined ? !this.outlineVisible
: !!show
;
6150 this.outlineVisible
= show
;
6151 this.gridLayout
.layout( show
? [ 1, 2 ] : [ 0, 1 ], [ 1 ] );
6158 * Get the outline widget.
6160 * @param {OO.ui.PageLayout} page Page to be selected
6161 * @return {OO.ui.PageLayout|null} Closest page to another
6163 OO
.ui
.BookletLayout
.prototype.getClosestPage = function ( page
) {
6164 var next
, prev
, level
,
6165 pages
= this.stackLayout
.getItems(),
6166 index
= $.inArray( page
, pages
);
6168 if ( index
!== -1 ) {
6169 next
= pages
[index
+ 1];
6170 prev
= pages
[index
- 1];
6171 // Prefer adjacent pages at the same level
6172 if ( this.outlined
) {
6173 level
= this.outlineWidget
.getItemFromData( page
.getName() ).getLevel();
6176 level
=== this.outlineWidget
.getItemFromData( prev
.getName() ).getLevel()
6182 level
=== this.outlineWidget
.getItemFromData( next
.getName() ).getLevel()
6188 return prev
|| next
|| null;
6192 * Get the outline widget.
6194 * @return {OO.ui.OutlineWidget|null} Outline widget, or null if boolet has no outline
6196 OO
.ui
.BookletLayout
.prototype.getOutline = function () {
6197 return this.outlineWidget
;
6201 * Get the outline controls widget. If the outline is not editable, null is returned.
6203 * @return {OO.ui.OutlineControlsWidget|null} The outline controls widget.
6205 OO
.ui
.BookletLayout
.prototype.getOutlineControls = function () {
6206 return this.outlineControlsWidget
;
6210 * Get a page by name.
6212 * @param {string} name Symbolic name of page
6213 * @return {OO.ui.PageLayout|undefined} Page, if found
6215 OO
.ui
.BookletLayout
.prototype.getPage = function ( name
) {
6216 return this.pages
[name
];
6220 * Get the current page name.
6222 * @return {string|null} Current page name
6224 OO
.ui
.BookletLayout
.prototype.getPageName = function () {
6225 return this.currentPageName
;
6229 * Add a page to the layout.
6231 * When pages are added with the same names as existing pages, the existing pages will be
6232 * automatically removed before the new pages are added.
6234 * @param {OO.ui.PageLayout[]} pages Pages to add
6235 * @param {number} index Index to insert pages after
6239 OO
.ui
.BookletLayout
.prototype.addPages = function ( pages
, index
) {
6240 var i
, len
, name
, page
, item
, currentIndex
,
6241 stackLayoutPages
= this.stackLayout
.getItems(),
6245 // Remove pages with same names
6246 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6248 name
= page
.getName();
6250 if ( Object
.prototype.hasOwnProperty
.call( this.pages
, name
) ) {
6251 // Correct the insertion index
6252 currentIndex
= $.inArray( this.pages
[name
], stackLayoutPages
);
6253 if ( currentIndex
!== -1 && currentIndex
+ 1 < index
) {
6256 remove
.push( this.pages
[name
] );
6259 if ( remove
.length
) {
6260 this.removePages( remove
);
6264 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6266 name
= page
.getName();
6267 this.pages
[page
.getName()] = page
;
6268 if ( this.outlined
) {
6269 item
= new OO
.ui
.OutlineItemWidget( name
, page
, { $: this.$ } );
6270 page
.setOutlineItem( item
);
6275 if ( this.outlined
&& items
.length
) {
6276 this.outlineWidget
.addItems( items
, index
);
6277 this.updateOutlineWidget();
6279 this.stackLayout
.addItems( pages
, index
);
6280 this.emit( 'add', pages
, index
);
6286 * Remove a page from the layout.
6291 OO
.ui
.BookletLayout
.prototype.removePages = function ( pages
) {
6292 var i
, len
, name
, page
,
6295 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6297 name
= page
.getName();
6298 delete this.pages
[name
];
6299 if ( this.outlined
) {
6300 items
.push( this.outlineWidget
.getItemFromData( name
) );
6301 page
.setOutlineItem( null );
6304 if ( this.outlined
&& items
.length
) {
6305 this.outlineWidget
.removeItems( items
);
6306 this.updateOutlineWidget();
6308 this.stackLayout
.removeItems( pages
);
6309 this.emit( 'remove', pages
);
6315 * Clear all pages from the layout.
6320 OO
.ui
.BookletLayout
.prototype.clearPages = function () {
6322 pages
= this.stackLayout
.getItems();
6325 this.currentPageName
= null;
6326 if ( this.outlined
) {
6327 this.outlineWidget
.clearItems();
6328 for ( i
= 0, len
= pages
.length
; i
< len
; i
++ ) {
6329 pages
[i
].setOutlineItem( null );
6332 this.stackLayout
.clearItems();
6334 this.emit( 'remove', pages
);
6340 * Set the current page by name.
6343 * @param {string} name Symbolic name of page
6345 OO
.ui
.BookletLayout
.prototype.setPage = function ( name
) {
6348 page
= this.pages
[name
];
6350 if ( name
!== this.currentPageName
) {
6351 if ( this.outlined
) {
6352 selectedItem
= this.outlineWidget
.getSelectedItem();
6353 if ( selectedItem
&& selectedItem
.getData() !== name
) {
6354 this.outlineWidget
.selectItem( this.outlineWidget
.getItemFromData( name
) );
6358 if ( this.currentPageName
&& this.pages
[this.currentPageName
] ) {
6359 this.pages
[this.currentPageName
].setActive( false );
6360 // Blur anything focused if the next page doesn't have anything focusable - this
6361 // is not needed if the next page has something focusable because once it is focused
6362 // this blur happens automatically
6363 if ( this.autoFocus
&& !page
.$element
.find( ':input' ).length
) {
6364 $focused
= this.pages
[this.currentPageName
].$element
.find( ':focus' );
6365 if ( $focused
.length
) {
6370 this.currentPageName
= name
;
6371 this.stackLayout
.setItem( page
);
6372 page
.setActive( true );
6373 this.emit( 'set', page
);
6379 * Call this after adding or removing items from the OutlineWidget.
6383 OO
.ui
.BookletLayout
.prototype.updateOutlineWidget = function () {
6384 // Auto-select first item when nothing is selected anymore
6385 if ( !this.outlineWidget
.getSelectedItem() ) {
6386 this.outlineWidget
.selectItem( this.outlineWidget
.getFirstSelectableItem() );
6393 * Layout made of a field and optional label.
6396 * @extends OO.ui.Layout
6397 * @mixins OO.ui.LabelElement
6399 * Available label alignment modes include:
6400 * - left: Label is before the field and aligned away from it, best for when the user will be
6401 * scanning for a specific label in a form with many fields
6402 * - right: Label is before the field and aligned toward it, best for forms the user is very
6403 * familiar with and will tab through field checking quickly to verify which field they are in
6404 * - top: Label is before the field and above it, best for when the use will need to fill out all
6405 * fields from top to bottom in a form with few fields
6406 * - inline: Label is after the field and aligned toward it, best for small boolean fields like
6407 * checkboxes or radio buttons
6410 * @param {OO.ui.Widget} field Field widget
6411 * @param {Object} [config] Configuration options
6412 * @cfg {string} [align='left'] Alignment mode, either 'left', 'right', 'top' or 'inline'
6413 * @cfg {string} [help] Explanatory text shown as a '?' icon.
6415 OO
.ui
.FieldLayout
= function OoUiFieldLayout( field
, config
) {
6416 // Config initialization
6417 config
= $.extend( { align
: 'left' }, config
);
6419 // Parent constructor
6420 OO
.ui
.FieldLayout
.super.call( this, config
);
6422 // Mixin constructors
6423 OO
.ui
.LabelElement
.call( this, config
);
6426 this.$field
= this.$( '<div>' );
6429 if ( config
.help
) {
6430 this.popupButtonWidget
= new OO
.ui
.PopupButtonWidget( {
6437 this.popupButtonWidget
.getPopup().$body
.append( this.$( '<span>' ).text( config
.help
) );
6438 this.$help
= this.popupButtonWidget
.$element
;
6440 this.$help
= this.$( '<div>' );
6444 if ( this.field
instanceof OO
.ui
.InputWidget
) {
6445 this.$label
.on( 'click', OO
.ui
.bind( this.onLabelClick
, this ) );
6447 this.field
.connect( this, { disable
: 'onFieldDisable' } );
6450 this.$element
.addClass( 'oo-ui-fieldLayout' );
6452 .addClass( 'oo-ui-fieldLayout-field' )
6453 .toggleClass( 'oo-ui-fieldLayout-disable', this.field
.isDisabled() )
6454 .append( this.field
.$element
);
6455 this.setAlignment( config
.align
);
6460 OO
.inheritClass( OO
.ui
.FieldLayout
, OO
.ui
.Layout
);
6461 OO
.mixinClass( OO
.ui
.FieldLayout
, OO
.ui
.LabelElement
);
6466 * Handle field disable events.
6468 * @param {boolean} value Field is disabled
6470 OO
.ui
.FieldLayout
.prototype.onFieldDisable = function ( value
) {
6471 this.$element
.toggleClass( 'oo-ui-fieldLayout-disabled', value
);
6475 * Handle label mouse click events.
6477 * @param {jQuery.Event} e Mouse click event
6479 OO
.ui
.FieldLayout
.prototype.onLabelClick = function () {
6480 this.field
.simulateLabelClick();
6487 * @return {OO.ui.Widget} Field widget
6489 OO
.ui
.FieldLayout
.prototype.getField = function () {
6494 * Set the field alignment mode.
6496 * @param {string} value Alignment mode, either 'left', 'right', 'top' or 'inline'
6499 OO
.ui
.FieldLayout
.prototype.setAlignment = function ( value
) {
6500 if ( value
!== this.align
) {
6501 // Default to 'left'
6502 if ( [ 'left', 'right', 'top', 'inline' ].indexOf( value
) === -1 ) {
6506 if ( value
=== 'inline' ) {
6507 this.$element
.append( this.$field
, this.$label
, this.$help
);
6509 this.$element
.append( this.$help
, this.$label
, this.$field
);
6513 this.$element
.removeClass( 'oo-ui-fieldLayout-align-' + this.align
);
6516 // The following classes can be used here:
6517 // oo-ui-fieldLayout-align-left
6518 // oo-ui-fieldLayout-align-right
6519 // oo-ui-fieldLayout-align-top
6520 // oo-ui-fieldLayout-align-inline
6521 this.$element
.addClass( 'oo-ui-fieldLayout-align-' + this.align
);
6528 * Layout made of a fieldset and optional legend.
6530 * Just add OO.ui.FieldLayout items.
6533 * @extends OO.ui.Layout
6534 * @mixins OO.ui.LabelElement
6535 * @mixins OO.ui.IconElement
6536 * @mixins OO.ui.GroupElement
6539 * @param {Object} [config] Configuration options
6540 * @cfg {string} [icon] Symbolic icon name
6541 * @cfg {OO.ui.FieldLayout[]} [items] Items to add
6543 OO
.ui
.FieldsetLayout
= function OoUiFieldsetLayout( config
) {
6544 // Config initialization
6545 config
= config
|| {};
6547 // Parent constructor
6548 OO
.ui
.FieldsetLayout
.super.call( this, config
);
6550 // Mixin constructors
6551 OO
.ui
.IconElement
.call( this, config
);
6552 OO
.ui
.LabelElement
.call( this, config
);
6553 OO
.ui
.GroupElement
.call( this, config
);
6557 .addClass( 'oo-ui-fieldsetLayout' )
6558 .prepend( this.$icon
, this.$label
, this.$group
);
6559 if ( $.isArray( config
.items
) ) {
6560 this.addItems( config
.items
);
6566 OO
.inheritClass( OO
.ui
.FieldsetLayout
, OO
.ui
.Layout
);
6567 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.IconElement
);
6568 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.LabelElement
);
6569 OO
.mixinClass( OO
.ui
.FieldsetLayout
, OO
.ui
.GroupElement
);
6571 /* Static Properties */
6573 OO
.ui
.FieldsetLayout
.static.tagName
= 'div';
6576 * Layout with an HTML form.
6579 * @extends OO.ui.Layout
6582 * @param {Object} [config] Configuration options
6584 OO
.ui
.FormLayout
= function OoUiFormLayout( config
) {
6585 // Configuration initialization
6586 config
= config
|| {};
6588 // Parent constructor
6589 OO
.ui
.FormLayout
.super.call( this, config
);
6592 this.$element
.on( 'submit', OO
.ui
.bind( this.onFormSubmit
, this ) );
6595 this.$element
.addClass( 'oo-ui-formLayout' );
6600 OO
.inheritClass( OO
.ui
.FormLayout
, OO
.ui
.Layout
);
6608 /* Static Properties */
6610 OO
.ui
.FormLayout
.static.tagName
= 'form';
6615 * Handle form submit events.
6617 * @param {jQuery.Event} e Submit event
6620 OO
.ui
.FormLayout
.prototype.onFormSubmit = function () {
6621 this.emit( 'submit' );
6626 * Layout made of proportionally sized columns and rows.
6629 * @extends OO.ui.Layout
6632 * @param {OO.ui.PanelLayout[]} panels Panels in the grid
6633 * @param {Object} [config] Configuration options
6634 * @cfg {number[]} [widths] Widths of columns as ratios
6635 * @cfg {number[]} [heights] Heights of columns as ratios
6637 OO
.ui
.GridLayout
= function OoUiGridLayout( panels
, config
) {
6640 // Config initialization
6641 config
= config
|| {};
6643 // Parent constructor
6644 OO
.ui
.GridLayout
.super.call( this, config
);
6652 this.$element
.addClass( 'oo-ui-gridLayout' );
6653 for ( i
= 0, len
= panels
.length
; i
< len
; i
++ ) {
6654 this.panels
.push( panels
[i
] );
6655 this.$element
.append( panels
[i
].$element
);
6657 if ( config
.widths
|| config
.heights
) {
6658 this.layout( config
.widths
|| [ 1 ], config
.heights
|| [ 1 ] );
6660 // Arrange in columns by default
6662 for ( i
= 0, len
= this.panels
.length
; i
< len
; i
++ ) {
6665 this.layout( widths
, [ 1 ] );
6671 OO
.inheritClass( OO
.ui
.GridLayout
, OO
.ui
.Layout
);
6683 /* Static Properties */
6685 OO
.ui
.GridLayout
.static.tagName
= 'div';
6690 * Set grid dimensions.
6692 * @param {number[]} widths Widths of columns as ratios
6693 * @param {number[]} heights Heights of rows as ratios
6695 * @throws {Error} If grid is not large enough to fit all panels
6697 OO
.ui
.GridLayout
.prototype.layout = function ( widths
, heights
) {
6701 cols
= widths
.length
,
6702 rows
= heights
.length
;
6704 // Verify grid is big enough to fit panels
6705 if ( cols
* rows
< this.panels
.length
) {
6706 throw new Error( 'Grid is not large enough to fit ' + this.panels
.length
+ 'panels' );
6709 // Sum up denominators
6710 for ( x
= 0; x
< cols
; x
++ ) {
6713 for ( y
= 0; y
< rows
; y
++ ) {
6719 for ( x
= 0; x
< cols
; x
++ ) {
6720 this.widths
[x
] = widths
[x
] / xd
;
6722 for ( y
= 0; y
< rows
; y
++ ) {
6723 this.heights
[y
] = heights
[y
] / yd
;
6727 this.emit( 'layout' );
6731 * Update panel positions and sizes.
6735 OO
.ui
.GridLayout
.prototype.update = function () {
6743 cols
= this.widths
.length
,
6744 rows
= this.heights
.length
;
6746 for ( y
= 0; y
< rows
; y
++ ) {
6747 height
= this.heights
[y
];
6748 for ( x
= 0; x
< cols
; x
++ ) {
6749 panel
= this.panels
[i
];
6750 width
= this.widths
[x
];
6752 width
: Math
.round( width
* 100 ) + '%',
6753 height
: Math
.round( height
* 100 ) + '%',
6754 top
: Math
.round( top
* 100 ) + '%',
6755 // HACK: Work around IE bug by setting visibility: hidden; if width or height is zero
6756 visibility
: width
=== 0 || height
=== 0 ? 'hidden' : ''
6759 if ( OO
.ui
.Element
.getDir( this.$.context
) === 'rtl' ) {
6760 dimensions
.right
= Math
.round( left
* 100 ) + '%';
6762 dimensions
.left
= Math
.round( left
* 100 ) + '%';
6764 panel
.$element
.css( dimensions
);
6772 this.emit( 'update' );
6776 * Get a panel at a given position.
6778 * The x and y position is affected by the current grid layout.
6780 * @param {number} x Horizontal position
6781 * @param {number} y Vertical position
6782 * @return {OO.ui.PanelLayout} The panel at the given postion
6784 OO
.ui
.GridLayout
.prototype.getPanel = function ( x
, y
) {
6785 return this.panels
[( x
* this.widths
.length
) + y
];
6789 * Layout that expands to cover the entire area of its parent, with optional scrolling and padding.
6792 * @extends OO.ui.Layout
6795 * @param {Object} [config] Configuration options
6796 * @cfg {boolean} [scrollable=false] Allow vertical scrolling
6797 * @cfg {boolean} [padded=false] Pad the content from the edges
6798 * @cfg {boolean} [expanded=true] Expand size to fill the entire parent element
6800 OO
.ui
.PanelLayout
= function OoUiPanelLayout( config
) {
6801 // Config initialization
6802 config
= config
|| {};
6804 // Parent constructor
6805 OO
.ui
.PanelLayout
.super.call( this, config
);
6808 this.$element
.addClass( 'oo-ui-panelLayout' );
6809 if ( config
.scrollable
) {
6810 this.$element
.addClass( 'oo-ui-panelLayout-scrollable' );
6813 if ( config
.padded
) {
6814 this.$element
.addClass( 'oo-ui-panelLayout-padded' );
6817 if ( config
.expanded
=== undefined || config
.expanded
) {
6818 this.$element
.addClass( 'oo-ui-panelLayout-expanded' );
6824 OO
.inheritClass( OO
.ui
.PanelLayout
, OO
.ui
.Layout
);
6827 * Page within an booklet layout.
6830 * @extends OO.ui.PanelLayout
6833 * @param {string} name Unique symbolic name of page
6834 * @param {Object} [config] Configuration options
6835 * @param {string} [outlineItem] Outline item widget
6837 OO
.ui
.PageLayout
= function OoUiPageLayout( name
, config
) {
6838 // Configuration initialization
6839 config
= $.extend( { scrollable
: true }, config
);
6841 // Parent constructor
6842 OO
.ui
.PageLayout
.super.call( this, config
);
6846 this.outlineItem
= config
.outlineItem
|| null;
6847 this.active
= false;
6850 this.$element
.addClass( 'oo-ui-pageLayout' );
6855 OO
.inheritClass( OO
.ui
.PageLayout
, OO
.ui
.PanelLayout
);
6861 * @param {boolean} active Page is active
6869 * @return {string} Symbolic name of page
6871 OO
.ui
.PageLayout
.prototype.getName = function () {
6876 * Check if page is active.
6878 * @return {boolean} Page is active
6880 OO
.ui
.PageLayout
.prototype.isActive = function () {
6887 * @return {OO.ui.OutlineItemWidget|null} Outline item widget
6889 OO
.ui
.PageLayout
.prototype.getOutlineItem = function () {
6890 return this.outlineItem
;
6896 * @localdoc Subclasses should override #setupOutlineItem instead of this method to adjust the
6897 * outline item as desired; this method is called for setting (with an object) and unsetting
6898 * (with null) and overriding methods would have to check the value of `outlineItem` to avoid
6899 * operating on null instead of an OO.ui.OutlineItemWidget object.
6901 * @param {OO.ui.OutlineItemWidget|null} outlineItem Outline item widget, null to clear
6904 OO
.ui
.PageLayout
.prototype.setOutlineItem = function ( outlineItem
) {
6905 this.outlineItem
= outlineItem
|| null;
6906 if ( outlineItem
) {
6907 this.setupOutlineItem();
6913 * Setup outline item.
6915 * @localdoc Subclasses should override this method to adjust the outline item as desired.
6917 * @param {OO.ui.OutlineItemWidget} outlineItem Outline item widget to setup
6920 OO
.ui
.PageLayout
.prototype.setupOutlineItem = function () {
6925 * Set page active state.
6927 * @param {boolean} Page is active
6930 OO
.ui
.PageLayout
.prototype.setActive = function ( active
) {
6933 if ( active
!== this.active
) {
6934 this.active
= active
;
6935 this.$element
.toggleClass( 'oo-ui-pageLayout-active', active
);
6936 this.emit( 'active', this.active
);
6941 * Layout containing a series of mutually exclusive pages.
6944 * @extends OO.ui.PanelLayout
6945 * @mixins OO.ui.GroupElement
6948 * @param {Object} [config] Configuration options
6949 * @cfg {boolean} [continuous=false] Show all pages, one after another
6950 * @cfg {string} [icon=''] Symbolic icon name
6951 * @cfg {OO.ui.Layout[]} [items] Layouts to add
6953 OO
.ui
.StackLayout
= function OoUiStackLayout( config
) {
6954 // Config initialization
6955 config
= $.extend( { scrollable
: true }, config
);
6957 // Parent constructor
6958 OO
.ui
.StackLayout
.super.call( this, config
);
6960 // Mixin constructors
6961 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
6964 this.currentItem
= null;
6965 this.continuous
= !!config
.continuous
;
6968 this.$element
.addClass( 'oo-ui-stackLayout' );
6969 if ( this.continuous
) {
6970 this.$element
.addClass( 'oo-ui-stackLayout-continuous' );
6972 if ( $.isArray( config
.items
) ) {
6973 this.addItems( config
.items
);
6979 OO
.inheritClass( OO
.ui
.StackLayout
, OO
.ui
.PanelLayout
);
6980 OO
.mixinClass( OO
.ui
.StackLayout
, OO
.ui
.GroupElement
);
6986 * @param {OO.ui.Layout|null} item Current item or null if there is no longer a layout shown
6992 * Get the current item.
6994 * @return {OO.ui.Layout|null}
6996 OO
.ui
.StackLayout
.prototype.getCurrentItem = function () {
6997 return this.currentItem
;
7001 * Unset the current item.
7004 * @param {OO.ui.StackLayout} layout
7007 OO
.ui
.StackLayout
.prototype.unsetCurrentItem = function () {
7008 var prevItem
= this.currentItem
;
7009 if ( prevItem
=== null ) {
7013 this.currentItem
= null;
7014 this.emit( 'set', null );
7020 * Adding an existing item (by value) will move it.
7022 * @param {OO.ui.Layout[]} items Items to add
7023 * @param {number} [index] Index to insert items after
7026 OO
.ui
.StackLayout
.prototype.addItems = function ( items
, index
) {
7028 OO
.ui
.GroupElement
.prototype.addItems
.call( this, items
, index
);
7030 if ( !this.currentItem
&& items
.length
) {
7031 this.setItem( items
[0] );
7040 * Items will be detached, not removed, so they can be used later.
7042 * @param {OO.ui.Layout[]} items Items to remove
7046 OO
.ui
.StackLayout
.prototype.removeItems = function ( items
) {
7048 OO
.ui
.GroupElement
.prototype.removeItems
.call( this, items
);
7050 if ( $.inArray( this.currentItem
, items
) !== -1 ) {
7051 if ( this.items
.length
) {
7052 this.setItem( this.items
[0] );
7054 this.unsetCurrentItem();
7064 * Items will be detached, not removed, so they can be used later.
7069 OO
.ui
.StackLayout
.prototype.clearItems = function () {
7070 this.unsetCurrentItem();
7071 OO
.ui
.GroupElement
.prototype.clearItems
.call( this );
7079 * Any currently shown item will be hidden.
7081 * FIXME: If the passed item to show has not been added in the items list, then
7082 * this method drops it and unsets the current item.
7084 * @param {OO.ui.Layout} item Item to show
7088 OO
.ui
.StackLayout
.prototype.setItem = function ( item
) {
7091 if ( item
!== this.currentItem
) {
7092 if ( !this.continuous
) {
7093 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7094 this.items
[i
].$element
.css( 'display', '' );
7097 if ( $.inArray( item
, this.items
) !== -1 ) {
7098 if ( !this.continuous
) {
7099 item
.$element
.css( 'display', 'block' );
7101 this.currentItem
= item
;
7102 this.emit( 'set', item
);
7104 this.unsetCurrentItem();
7112 * Horizontal bar layout of tools as icon buttons.
7115 * @extends OO.ui.ToolGroup
7118 * @param {OO.ui.Toolbar} toolbar
7119 * @param {Object} [config] Configuration options
7121 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
7122 // Parent constructor
7123 OO
.ui
.BarToolGroup
.super.call( this, toolbar
, config
);
7126 this.$element
.addClass( 'oo-ui-barToolGroup' );
7131 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
7133 /* Static Properties */
7135 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
7137 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
7139 OO
.ui
.BarToolGroup
.static.name
= 'bar';
7142 * Popup list of tools with an icon and optional label.
7146 * @extends OO.ui.ToolGroup
7147 * @mixins OO.ui.IconElement
7148 * @mixins OO.ui.IndicatorElement
7149 * @mixins OO.ui.LabelElement
7150 * @mixins OO.ui.TitledElement
7151 * @mixins OO.ui.ClippableElement
7154 * @param {OO.ui.Toolbar} toolbar
7155 * @param {Object} [config] Configuration options
7156 * @cfg {string} [header] Text to display at the top of the pop-up
7158 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
7159 // Configuration initialization
7160 config
= config
|| {};
7162 // Parent constructor
7163 OO
.ui
.PopupToolGroup
.super.call( this, toolbar
, config
);
7165 // Mixin constructors
7166 OO
.ui
.IconElement
.call( this, config
);
7167 OO
.ui
.IndicatorElement
.call( this, config
);
7168 OO
.ui
.LabelElement
.call( this, config
);
7169 OO
.ui
.TitledElement
.call( this, config
);
7170 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
7173 this.active
= false;
7174 this.dragging
= false;
7175 this.onBlurHandler
= OO
.ui
.bind( this.onBlur
, this );
7176 this.$handle
= this.$( '<span>' );
7180 'mousedown touchstart': OO
.ui
.bind( this.onHandlePointerDown
, this ),
7181 'mouseup touchend': OO
.ui
.bind( this.onHandlePointerUp
, this )
7186 .addClass( 'oo-ui-popupToolGroup-handle' )
7187 .append( this.$icon
, this.$label
, this.$indicator
);
7188 // If the pop-up should have a header, add it to the top of the toolGroup.
7189 // Note: If this feature is useful for other widgets, we could abstract it into an
7190 // OO.ui.HeaderedElement mixin constructor.
7191 if ( config
.header
!== undefined ) {
7193 .prepend( this.$( '<span>' )
7194 .addClass( 'oo-ui-popupToolGroup-header' )
7195 .text( config
.header
)
7199 .addClass( 'oo-ui-popupToolGroup' )
7200 .prepend( this.$handle
);
7205 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
7206 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IconElement
);
7207 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.IndicatorElement
);
7208 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.LabelElement
);
7209 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.TitledElement
);
7210 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ClippableElement
);
7212 /* Static Properties */
7219 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
7221 OO
.ui
.PopupToolGroup
.super.prototype.setDisabled
.apply( this, arguments
);
7223 if ( this.isDisabled() && this.isElementAttached() ) {
7224 this.setActive( false );
7229 * Handle focus being lost.
7231 * The event is actually generated from a mouseup, so it is not a normal blur event object.
7233 * @param {jQuery.Event} e Mouse up event
7235 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
7236 // Only deactivate when clicking outside the dropdown element
7237 if ( this.$( e
.target
).closest( '.oo-ui-popupToolGroup' )[0] !== this.$element
[0] ) {
7238 this.setActive( false );
7245 OO
.ui
.PopupToolGroup
.prototype.onPointerUp = function ( e
) {
7246 // e.which is 0 for touch events, 1 for left mouse button
7247 if ( !this.isDisabled() && e
.which
<= 1 ) {
7248 this.setActive( false );
7250 return OO
.ui
.PopupToolGroup
.super.prototype.onPointerUp
.call( this, e
);
7254 * Handle mouse up events.
7256 * @param {jQuery.Event} e Mouse up event
7258 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerUp = function () {
7263 * Handle mouse down events.
7265 * @param {jQuery.Event} e Mouse down event
7267 OO
.ui
.PopupToolGroup
.prototype.onHandlePointerDown = function ( e
) {
7268 // e.which is 0 for touch events, 1 for left mouse button
7269 if ( !this.isDisabled() && e
.which
<= 1 ) {
7270 this.setActive( !this.active
);
7276 * Switch into active mode.
7278 * When active, mouseup events anywhere in the document will trigger deactivation.
7280 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
7282 if ( this.active
!== value
) {
7283 this.active
= value
;
7285 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
7287 // Try anchoring the popup to the left first
7288 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
7289 this.toggleClipping( true );
7290 if ( this.isClippedHorizontally() ) {
7291 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
7292 this.toggleClipping( false );
7294 .removeClass( 'oo-ui-popupToolGroup-left' )
7295 .addClass( 'oo-ui-popupToolGroup-right' );
7296 this.toggleClipping( true );
7299 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
7300 this.$element
.removeClass(
7301 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
7303 this.toggleClipping( false );
7309 * Drop down list layout of tools as labeled icon buttons.
7312 * @extends OO.ui.PopupToolGroup
7315 * @param {OO.ui.Toolbar} toolbar
7316 * @param {Object} [config] Configuration options
7318 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
7319 // Parent constructor
7320 OO
.ui
.ListToolGroup
.super.call( this, toolbar
, config
);
7323 this.$element
.addClass( 'oo-ui-listToolGroup' );
7328 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
7330 /* Static Properties */
7332 OO
.ui
.ListToolGroup
.static.accelTooltips
= true;
7334 OO
.ui
.ListToolGroup
.static.name
= 'list';
7337 * Drop down menu layout of tools as selectable menu items.
7340 * @extends OO.ui.PopupToolGroup
7343 * @param {OO.ui.Toolbar} toolbar
7344 * @param {Object} [config] Configuration options
7346 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
7347 // Configuration initialization
7348 config
= config
|| {};
7350 // Parent constructor
7351 OO
.ui
.MenuToolGroup
.super.call( this, toolbar
, config
);
7354 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
7357 this.$element
.addClass( 'oo-ui-menuToolGroup' );
7362 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
7364 /* Static Properties */
7366 OO
.ui
.MenuToolGroup
.static.accelTooltips
= true;
7368 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
7373 * Handle the toolbar state being updated.
7375 * When the state changes, the title of each active item in the menu will be joined together and
7376 * used as a label for the group. The label will be empty if none of the items are active.
7378 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
7382 for ( name
in this.tools
) {
7383 if ( this.tools
[name
].isActive() ) {
7384 labelTexts
.push( this.tools
[name
].getTitle() );
7388 this.setLabel( labelTexts
.join( ', ' ) || ' ' );
7392 * Tool that shows a popup when selected.
7396 * @extends OO.ui.Tool
7397 * @mixins OO.ui.PopupElement
7400 * @param {OO.ui.Toolbar} toolbar
7401 * @param {Object} [config] Configuration options
7403 OO
.ui
.PopupTool
= function OoUiPopupTool( toolbar
, config
) {
7404 // Parent constructor
7405 OO
.ui
.PopupTool
.super.call( this, toolbar
, config
);
7407 // Mixin constructors
7408 OO
.ui
.PopupElement
.call( this, config
);
7412 .addClass( 'oo-ui-popupTool' )
7413 .append( this.popup
.$element
);
7418 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
7419 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.PopupElement
);
7424 * Handle the tool being selected.
7428 OO
.ui
.PopupTool
.prototype.onSelect = function () {
7429 if ( !this.isDisabled() ) {
7430 this.popup
.toggle();
7432 this.setActive( false );
7437 * Handle the toolbar state being updated.
7441 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
7442 this.setActive( false );
7446 * Mixin for OO.ui.Widget subclasses to provide OO.ui.GroupElement.
7448 * Use together with OO.ui.ItemWidget to make disabled state inheritable.
7452 * @extends OO.ui.GroupElement
7455 * @param {Object} [config] Configuration options
7457 OO
.ui
.GroupWidget
= function OoUiGroupWidget( config
) {
7458 // Parent constructor
7459 OO
.ui
.GroupWidget
.super.call( this, config
);
7464 OO
.inheritClass( OO
.ui
.GroupWidget
, OO
.ui
.GroupElement
);
7469 * Set the disabled state of the widget.
7471 * This will also update the disabled state of child widgets.
7473 * @param {boolean} disabled Disable widget
7476 OO
.ui
.GroupWidget
.prototype.setDisabled = function ( disabled
) {
7480 // Note: Calling #setDisabled this way assumes this is mixed into an OO.ui.Widget
7481 OO
.ui
.Widget
.prototype.setDisabled
.call( this, disabled
);
7483 // During construction, #setDisabled is called before the OO.ui.GroupElement constructor
7485 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
7486 this.items
[i
].updateDisabled();
7494 * Mixin for widgets used as items in widgets that inherit OO.ui.GroupWidget.
7496 * Item widgets have a reference to a OO.ui.GroupWidget while they are attached to the group. This
7497 * allows bidrectional communication.
7499 * Use together with OO.ui.GroupWidget to make disabled state inheritable.
7506 OO
.ui
.ItemWidget
= function OoUiItemWidget() {
7513 * Check if widget is disabled.
7515 * Checks parent if present, making disabled state inheritable.
7517 * @return {boolean} Widget is disabled
7519 OO
.ui
.ItemWidget
.prototype.isDisabled = function () {
7520 return this.disabled
||
7521 ( this.elementGroup
instanceof OO
.ui
.Widget
&& this.elementGroup
.isDisabled() );
7525 * Set group element is in.
7527 * @param {OO.ui.GroupElement|null} group Group element, null if none
7530 OO
.ui
.ItemWidget
.prototype.setElementGroup = function ( group
) {
7532 // Note: Calling #setElementGroup this way assumes this is mixed into an OO.ui.Element
7533 OO
.ui
.Element
.prototype.setElementGroup
.call( this, group
);
7535 // Initialize item disabled states
7536 this.updateDisabled();
7542 * Mixin that adds a menu showing suggested values for a text input.
7544 * Subclasses must handle `select` and `choose` events on #lookupMenu to make use of selections.
7550 * @param {OO.ui.TextInputWidget} input Input widget
7551 * @param {Object} [config] Configuration options
7552 * @cfg {jQuery} [$overlay=this.$( 'body' )] Overlay layer
7554 OO
.ui
.LookupInputWidget
= function OoUiLookupInputWidget( input
, config
) {
7555 // Config intialization
7556 config
= config
|| {};
7559 this.lookupInput
= input
;
7560 this.$overlay
= config
.$overlay
|| this.$( 'body,.oo-ui-window-overlay' ).last();
7561 this.lookupMenu
= new OO
.ui
.TextInputMenuWidget( this, {
7562 $: OO
.ui
.Element
.getJQuery( this.$overlay
),
7563 input
: this.lookupInput
,
7564 $container
: config
.$container
7566 this.lookupCache
= {};
7567 this.lookupQuery
= null;
7568 this.lookupRequest
= null;
7569 this.populating
= false;
7572 this.$overlay
.append( this.lookupMenu
.$element
);
7574 this.lookupInput
.$input
.on( {
7575 focus
: OO
.ui
.bind( this.onLookupInputFocus
, this ),
7576 blur
: OO
.ui
.bind( this.onLookupInputBlur
, this ),
7577 mousedown
: OO
.ui
.bind( this.onLookupInputMouseDown
, this )
7579 this.lookupInput
.connect( this, { change
: 'onLookupInputChange' } );
7582 this.$element
.addClass( 'oo-ui-lookupWidget' );
7583 this.lookupMenu
.$element
.addClass( 'oo-ui-lookupWidget-menu' );
7589 * Handle input focus event.
7591 * @param {jQuery.Event} e Input focus event
7593 OO
.ui
.LookupInputWidget
.prototype.onLookupInputFocus = function () {
7594 this.openLookupMenu();
7598 * Handle input blur event.
7600 * @param {jQuery.Event} e Input blur event
7602 OO
.ui
.LookupInputWidget
.prototype.onLookupInputBlur = function () {
7603 this.lookupMenu
.toggle( false );
7607 * Handle input mouse down event.
7609 * @param {jQuery.Event} e Input mouse down event
7611 OO
.ui
.LookupInputWidget
.prototype.onLookupInputMouseDown = function () {
7612 this.openLookupMenu();
7616 * Handle input change event.
7618 * @param {string} value New input value
7620 OO
.ui
.LookupInputWidget
.prototype.onLookupInputChange = function () {
7621 this.openLookupMenu();
7627 * @return {OO.ui.TextInputMenuWidget}
7629 OO
.ui
.LookupInputWidget
.prototype.getLookupMenu = function () {
7630 return this.lookupMenu
;
7638 OO
.ui
.LookupInputWidget
.prototype.openLookupMenu = function () {
7639 var value
= this.lookupInput
.getValue();
7641 if ( this.lookupMenu
.$input
.is( ':focus' ) && $.trim( value
) !== '' ) {
7642 this.populateLookupMenu();
7643 this.lookupMenu
.toggle( true );
7654 * Populate lookup menu with current information.
7658 OO
.ui
.LookupInputWidget
.prototype.populateLookupMenu = function () {
7661 if ( !this.populating
) {
7662 this.populating
= true;
7663 this.getLookupMenuItems()
7664 .done( function ( items
) {
7665 widget
.lookupMenu
.clearItems();
7666 if ( items
.length
) {
7670 widget
.initializeLookupMenuSelection();
7671 widget
.openLookupMenu();
7673 widget
.lookupMenu
.toggle( true );
7675 widget
.populating
= false;
7677 .fail( function () {
7678 widget
.lookupMenu
.clearItems();
7679 widget
.populating
= false;
7687 * Set selection in the lookup menu with current information.
7691 OO
.ui
.LookupInputWidget
.prototype.initializeLookupMenuSelection = function () {
7692 if ( !this.lookupMenu
.getSelectedItem() ) {
7693 this.lookupMenu
.selectItem( this.lookupMenu
.getFirstSelectableItem() );
7695 this.lookupMenu
.highlightItem( this.lookupMenu
.getSelectedItem() );
7699 * Get lookup menu items for the current query.
7701 * @return {jQuery.Promise} Promise object which will be passed menu items as the first argument
7704 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItems = function () {
7706 value
= this.lookupInput
.getValue(),
7707 deferred
= $.Deferred();
7709 if ( value
&& value
!== this.lookupQuery
) {
7710 // Abort current request if query has changed
7711 if ( this.lookupRequest
) {
7712 this.lookupRequest
.abort();
7713 this.lookupQuery
= null;
7714 this.lookupRequest
= null;
7716 if ( value
in this.lookupCache
) {
7717 deferred
.resolve( this.getLookupMenuItemsFromData( this.lookupCache
[value
] ) );
7719 this.lookupQuery
= value
;
7720 this.lookupRequest
= this.getLookupRequest()
7721 .always( function () {
7722 widget
.lookupQuery
= null;
7723 widget
.lookupRequest
= null;
7725 .done( function ( data
) {
7726 widget
.lookupCache
[value
] = widget
.getLookupCacheItemFromData( data
);
7727 deferred
.resolve( widget
.getLookupMenuItemsFromData( widget
.lookupCache
[value
] ) );
7729 .fail( function () {
7733 this.lookupRequest
.always( function () {
7734 widget
.popPending();
7738 return deferred
.promise();
7742 * Get a new request object of the current lookup query value.
7745 * @return {jqXHR} jQuery AJAX object, or promise object with an .abort() method
7747 OO
.ui
.LookupInputWidget
.prototype.getLookupRequest = function () {
7748 // Stub, implemented in subclass
7753 * Handle successful lookup request.
7755 * Overriding methods should call #populateLookupMenu when results are available and cache results
7756 * for future lookups in #lookupCache as an array of #OO.ui.MenuItemWidget objects.
7759 * @param {Mixed} data Response from server
7761 OO
.ui
.LookupInputWidget
.prototype.onLookupRequestDone = function () {
7762 // Stub, implemented in subclass
7766 * Get a list of menu item widgets from the data stored by the lookup request's done handler.
7769 * @param {Mixed} data Cached result data, usually an array
7770 * @return {OO.ui.MenuItemWidget[]} Menu items
7772 OO
.ui
.LookupInputWidget
.prototype.getLookupMenuItemsFromData = function () {
7773 // Stub, implemented in subclass
7778 * Set of controls for an OO.ui.OutlineWidget.
7780 * Controls include moving items up and down, removing items, and adding different kinds of items.
7783 * @extends OO.ui.Widget
7784 * @mixins OO.ui.GroupElement
7785 * @mixins OO.ui.IconElement
7788 * @param {OO.ui.OutlineWidget} outline Outline to control
7789 * @param {Object} [config] Configuration options
7791 OO
.ui
.OutlineControlsWidget
= function OoUiOutlineControlsWidget( outline
, config
) {
7792 // Configuration initialization
7793 config
= $.extend( { icon
: 'add-item' }, config
);
7795 // Parent constructor
7796 OO
.ui
.OutlineControlsWidget
.super.call( this, config
);
7798 // Mixin constructors
7799 OO
.ui
.GroupElement
.call( this, config
);
7800 OO
.ui
.IconElement
.call( this, config
);
7803 this.outline
= outline
;
7804 this.$movers
= this.$( '<div>' );
7805 this.upButton
= new OO
.ui
.ButtonWidget( {
7809 title
: OO
.ui
.msg( 'ooui-outline-control-move-up' )
7811 this.downButton
= new OO
.ui
.ButtonWidget( {
7815 title
: OO
.ui
.msg( 'ooui-outline-control-move-down' )
7817 this.removeButton
= new OO
.ui
.ButtonWidget( {
7821 title
: OO
.ui
.msg( 'ooui-outline-control-remove' )
7825 outline
.connect( this, {
7826 select
: 'onOutlineChange',
7827 add
: 'onOutlineChange',
7828 remove
: 'onOutlineChange'
7830 this.upButton
.connect( this, { click
: [ 'emit', 'move', -1 ] } );
7831 this.downButton
.connect( this, { click
: [ 'emit', 'move', 1 ] } );
7832 this.removeButton
.connect( this, { click
: [ 'emit', 'remove' ] } );
7835 this.$element
.addClass( 'oo-ui-outlineControlsWidget' );
7836 this.$group
.addClass( 'oo-ui-outlineControlsWidget-items' );
7838 .addClass( 'oo-ui-outlineControlsWidget-movers' )
7839 .append( this.removeButton
.$element
, this.upButton
.$element
, this.downButton
.$element
);
7840 this.$element
.append( this.$icon
, this.$group
, this.$movers
);
7845 OO
.inheritClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.Widget
);
7846 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.GroupElement
);
7847 OO
.mixinClass( OO
.ui
.OutlineControlsWidget
, OO
.ui
.IconElement
);
7853 * @param {number} places Number of places to move
7863 * Handle outline change events.
7865 OO
.ui
.OutlineControlsWidget
.prototype.onOutlineChange = function () {
7866 var i
, len
, firstMovable
, lastMovable
,
7867 items
= this.outline
.getItems(),
7868 selectedItem
= this.outline
.getSelectedItem(),
7869 movable
= selectedItem
&& selectedItem
.isMovable(),
7870 removable
= selectedItem
&& selectedItem
.isRemovable();
7875 while ( ++i
< len
) {
7876 if ( items
[i
].isMovable() ) {
7877 firstMovable
= items
[i
];
7883 if ( items
[i
].isMovable() ) {
7884 lastMovable
= items
[i
];
7889 this.upButton
.setDisabled( !movable
|| selectedItem
=== firstMovable
);
7890 this.downButton
.setDisabled( !movable
|| selectedItem
=== lastMovable
);
7891 this.removeButton
.setDisabled( !removable
);
7895 * Mixin for widgets with a boolean on/off state.
7901 * @param {Object} [config] Configuration options
7902 * @cfg {boolean} [value=false] Initial value
7904 OO
.ui
.ToggleWidget
= function OoUiToggleWidget( config
) {
7905 // Configuration initialization
7906 config
= config
|| {};
7912 this.$element
.addClass( 'oo-ui-toggleWidget' );
7913 this.setValue( !!config
.value
);
7920 * @param {boolean} value Changed value
7926 * Get the value of the toggle.
7930 OO
.ui
.ToggleWidget
.prototype.getValue = function () {
7935 * Set the value of the toggle.
7937 * @param {boolean} value New value
7941 OO
.ui
.ToggleWidget
.prototype.setValue = function ( value
) {
7943 if ( this.value
!== value
) {
7945 this.emit( 'change', value
);
7946 this.$element
.toggleClass( 'oo-ui-toggleWidget-on', value
);
7947 this.$element
.toggleClass( 'oo-ui-toggleWidget-off', !value
);
7953 * Group widget for multiple related buttons.
7955 * Use together with OO.ui.ButtonWidget.
7958 * @extends OO.ui.Widget
7959 * @mixins OO.ui.GroupElement
7962 * @param {Object} [config] Configuration options
7963 * @cfg {OO.ui.ButtonWidget} [items] Buttons to add
7965 OO
.ui
.ButtonGroupWidget
= function OoUiButtonGroupWidget( config
) {
7966 // Parent constructor
7967 OO
.ui
.ButtonGroupWidget
.super.call( this, config
);
7969 // Mixin constructors
7970 OO
.ui
.GroupElement
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
7973 this.$element
.addClass( 'oo-ui-buttonGroupWidget' );
7974 if ( $.isArray( config
.items
) ) {
7975 this.addItems( config
.items
);
7981 OO
.inheritClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.Widget
);
7982 OO
.mixinClass( OO
.ui
.ButtonGroupWidget
, OO
.ui
.GroupElement
);
7985 * Generic widget for buttons.
7988 * @extends OO.ui.Widget
7989 * @mixins OO.ui.ButtonElement
7990 * @mixins OO.ui.IconElement
7991 * @mixins OO.ui.IndicatorElement
7992 * @mixins OO.ui.LabelElement
7993 * @mixins OO.ui.TitledElement
7994 * @mixins OO.ui.FlaggedElement
7997 * @param {Object} [config] Configuration options
7998 * @cfg {string} [href] Hyperlink to visit when clicked
7999 * @cfg {string} [target] Target to open hyperlink in
8001 OO
.ui
.ButtonWidget
= function OoUiButtonWidget( config
) {
8002 // Configuration initialization
8003 config
= $.extend( { target
: '_blank' }, config
);
8005 // Parent constructor
8006 OO
.ui
.ButtonWidget
.super.call( this, config
);
8008 // Mixin constructors
8009 OO
.ui
.ButtonElement
.call( this, config
);
8010 OO
.ui
.IconElement
.call( this, config
);
8011 OO
.ui
.IndicatorElement
.call( this, config
);
8012 OO
.ui
.LabelElement
.call( this, config
);
8013 OO
.ui
.TitledElement
.call( this, config
, $.extend( {}, config
, { $titled
: this.$button
} ) );
8014 OO
.ui
.FlaggedElement
.call( this, config
);
8019 this.isHyperlink
= false;
8023 click
: OO
.ui
.bind( this.onClick
, this ),
8024 keypress
: OO
.ui
.bind( this.onKeyPress
, this )
8028 this.$button
.append( this.$icon
, this.$label
, this.$indicator
);
8030 .addClass( 'oo-ui-buttonWidget' )
8031 .append( this.$button
);
8032 this.setHref( config
.href
);
8033 this.setTarget( config
.target
);
8038 OO
.inheritClass( OO
.ui
.ButtonWidget
, OO
.ui
.Widget
);
8039 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.ButtonElement
);
8040 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IconElement
);
8041 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.IndicatorElement
);
8042 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.LabelElement
);
8043 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.TitledElement
);
8044 OO
.mixinClass( OO
.ui
.ButtonWidget
, OO
.ui
.FlaggedElement
);
8055 * Handles mouse click events.
8057 * @param {jQuery.Event} e Mouse click event
8060 OO
.ui
.ButtonWidget
.prototype.onClick = function () {
8061 if ( !this.isDisabled() ) {
8062 this.emit( 'click' );
8063 if ( this.isHyperlink
) {
8071 * Handles keypress events.
8073 * @param {jQuery.Event} e Keypress event
8076 OO
.ui
.ButtonWidget
.prototype.onKeyPress = function ( e
) {
8077 if ( !this.isDisabled() && ( e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
) ) {
8079 if ( this.isHyperlink
) {
8087 * Get hyperlink location.
8089 * @return {string} Hyperlink location
8091 OO
.ui
.ButtonWidget
.prototype.getHref = function () {
8096 * Get hyperlink target.
8098 * @return {string} Hyperlink target
8100 OO
.ui
.ButtonWidget
.prototype.getTarget = function () {
8105 * Set hyperlink location.
8107 * @param {string|null} href Hyperlink location, null to remove
8109 OO
.ui
.ButtonWidget
.prototype.setHref = function ( href
) {
8110 href
= typeof href
=== 'string' ? href
: null;
8112 if ( href
!== this.href
) {
8114 if ( href
!== null ) {
8115 this.$button
.attr( 'href', href
);
8116 this.isHyperlink
= true;
8118 this.$button
.removeAttr( 'href' );
8119 this.isHyperlink
= false;
8127 * Set hyperlink target.
8129 * @param {string|null} target Hyperlink target, null to remove
8131 OO
.ui
.ButtonWidget
.prototype.setTarget = function ( target
) {
8132 target
= typeof target
=== 'string' ? target
: null;
8134 if ( target
!== this.target
) {
8135 this.target
= target
;
8136 if ( target
!== null ) {
8137 this.$button
.attr( 'target', target
);
8139 this.$button
.removeAttr( 'target' );
8147 * Button widget that executes an action and is managed by an OO.ui.ActionSet.
8150 * @extends OO.ui.ButtonWidget
8153 * @param {Object} [config] Configuration options
8154 * @cfg {string} [action] Symbolic action name
8155 * @cfg {string[]} [modes] Symbolic mode names
8157 OO
.ui
.ActionWidget
= function OoUiActionWidget( config
) {
8158 // Config intialization
8159 config
= $.extend( { framed
: false }, config
);
8161 // Parent constructor
8162 OO
.ui
.ActionWidget
.super.call( this, config
);
8165 this.action
= config
.action
|| '';
8166 this.modes
= config
.modes
|| [];
8171 this.$element
.addClass( 'oo-ui-actionWidget' );
8176 OO
.inheritClass( OO
.ui
.ActionWidget
, OO
.ui
.ButtonWidget
);
8187 * Check if action is available in a certain mode.
8189 * @param {string} mode Name of mode
8190 * @return {boolean} Has mode
8192 OO
.ui
.ActionWidget
.prototype.hasMode = function ( mode
) {
8193 return this.modes
.indexOf( mode
) !== -1;
8197 * Get symbolic action name.
8201 OO
.ui
.ActionWidget
.prototype.getAction = function () {
8206 * Get symbolic action name.
8210 OO
.ui
.ActionWidget
.prototype.getModes = function () {
8211 return this.modes
.slice();
8215 * Emit a resize event if the size has changed.
8219 OO
.ui
.ActionWidget
.prototype.propagateResize = function () {
8222 if ( this.isElementAttached() ) {
8223 width
= this.$element
.width();
8224 height
= this.$element
.height();
8226 if ( width
!== this.width
|| height
!== this.height
) {
8228 this.height
= height
;
8229 this.emit( 'resize' );
8239 OO
.ui
.ActionWidget
.prototype.setIcon = function () {
8241 OO
.ui
.IconElement
.prototype.setIcon
.apply( this, arguments
);
8242 this.propagateResize();
8250 OO
.ui
.ActionWidget
.prototype.setLabel = function () {
8252 OO
.ui
.LabelElement
.prototype.setLabel
.apply( this, arguments
);
8253 this.propagateResize();
8261 OO
.ui
.ActionWidget
.prototype.setFlags = function () {
8263 OO
.ui
.FlaggedElement
.prototype.setFlags
.apply( this, arguments
);
8264 this.propagateResize();
8272 OO
.ui
.ActionWidget
.prototype.clearFlags = function () {
8274 OO
.ui
.FlaggedElement
.prototype.clearFlags
.apply( this, arguments
);
8275 this.propagateResize();
8281 * Toggle visibility of button.
8283 * @param {boolean} [show] Show button, omit to toggle visibility
8286 OO
.ui
.ActionWidget
.prototype.toggle = function () {
8288 OO
.ui
.ActionWidget
.super.prototype.toggle
.apply( this, arguments
);
8289 this.propagateResize();
8295 * Button that shows and hides a popup.
8298 * @extends OO.ui.ButtonWidget
8299 * @mixins OO.ui.PopupElement
8302 * @param {Object} [config] Configuration options
8304 OO
.ui
.PopupButtonWidget
= function OoUiPopupButtonWidget( config
) {
8305 // Parent constructor
8306 OO
.ui
.PopupButtonWidget
.super.call( this, config
);
8308 // Mixin constructors
8309 OO
.ui
.PopupElement
.call( this, config
);
8313 .addClass( 'oo-ui-popupButtonWidget' )
8314 .append( this.popup
.$element
);
8319 OO
.inheritClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.ButtonWidget
);
8320 OO
.mixinClass( OO
.ui
.PopupButtonWidget
, OO
.ui
.PopupElement
);
8325 * Handles mouse click events.
8327 * @param {jQuery.Event} e Mouse click event
8329 OO
.ui
.PopupButtonWidget
.prototype.onClick = function ( e
) {
8330 // Skip clicks within the popup
8331 if ( $.contains( this.popup
.$element
[0], e
.target
) ) {
8335 if ( !this.isDisabled() ) {
8336 this.popup
.toggle();
8338 OO
.ui
.PopupButtonWidget
.super.prototype.onClick
.call( this );
8344 * Button that toggles on and off.
8347 * @extends OO.ui.ButtonWidget
8348 * @mixins OO.ui.ToggleWidget
8351 * @param {Object} [config] Configuration options
8352 * @cfg {boolean} [value=false] Initial value
8354 OO
.ui
.ToggleButtonWidget
= function OoUiToggleButtonWidget( config
) {
8355 // Configuration initialization
8356 config
= config
|| {};
8358 // Parent constructor
8359 OO
.ui
.ToggleButtonWidget
.super.call( this, config
);
8361 // Mixin constructors
8362 OO
.ui
.ToggleWidget
.call( this, config
);
8365 this.$element
.addClass( 'oo-ui-toggleButtonWidget' );
8370 OO
.inheritClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ButtonWidget
);
8371 OO
.mixinClass( OO
.ui
.ToggleButtonWidget
, OO
.ui
.ToggleWidget
);
8378 OO
.ui
.ToggleButtonWidget
.prototype.onClick = function () {
8379 if ( !this.isDisabled() ) {
8380 this.setValue( !this.value
);
8384 return OO
.ui
.ToggleButtonWidget
.super.prototype.onClick
.call( this );
8390 OO
.ui
.ToggleButtonWidget
.prototype.setValue = function ( value
) {
8392 if ( value
!== this.value
) {
8393 this.setActive( value
);
8396 // Parent method (from mixin)
8397 OO
.ui
.ToggleWidget
.prototype.setValue
.call( this, value
);
8405 * See OO.ui.IconElement for more information.
8408 * @extends OO.ui.Widget
8409 * @mixins OO.ui.IconElement
8410 * @mixins OO.ui.TitledElement
8413 * @param {Object} [config] Configuration options
8415 OO
.ui
.IconWidget
= function OoUiIconWidget( config
) {
8416 // Config intialization
8417 config
= config
|| {};
8419 // Parent constructor
8420 OO
.ui
.IconWidget
.super.call( this, config
);
8422 // Mixin constructors
8423 OO
.ui
.IconElement
.call( this, $.extend( {}, config
, { $icon
: this.$element
} ) );
8424 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8427 this.$element
.addClass( 'oo-ui-iconWidget' );
8432 OO
.inheritClass( OO
.ui
.IconWidget
, OO
.ui
.Widget
);
8433 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.IconElement
);
8434 OO
.mixinClass( OO
.ui
.IconWidget
, OO
.ui
.TitledElement
);
8436 /* Static Properties */
8438 OO
.ui
.IconWidget
.static.tagName
= 'span';
8443 * See OO.ui.IndicatorElement for more information.
8446 * @extends OO.ui.Widget
8447 * @mixins OO.ui.IndicatorElement
8448 * @mixins OO.ui.TitledElement
8451 * @param {Object} [config] Configuration options
8453 OO
.ui
.IndicatorWidget
= function OoUiIndicatorWidget( config
) {
8454 // Config intialization
8455 config
= config
|| {};
8457 // Parent constructor
8458 OO
.ui
.IndicatorWidget
.super.call( this, config
);
8460 // Mixin constructors
8461 OO
.ui
.IndicatorElement
.call( this, $.extend( {}, config
, { $indicator
: this.$element
} ) );
8462 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$element
} ) );
8465 this.$element
.addClass( 'oo-ui-indicatorWidget' );
8470 OO
.inheritClass( OO
.ui
.IndicatorWidget
, OO
.ui
.Widget
);
8471 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.IndicatorElement
);
8472 OO
.mixinClass( OO
.ui
.IndicatorWidget
, OO
.ui
.TitledElement
);
8474 /* Static Properties */
8476 OO
.ui
.IndicatorWidget
.static.tagName
= 'span';
8479 * Inline menu of options.
8481 * Inline menus provide a control for accessing a menu and compose a menu within the widget, which
8482 * can be accessed using the #getMenu method.
8484 * Use with OO.ui.MenuOptionWidget.
8487 * @extends OO.ui.Widget
8488 * @mixins OO.ui.IconElement
8489 * @mixins OO.ui.IndicatorElement
8490 * @mixins OO.ui.LabelElement
8491 * @mixins OO.ui.TitledElement
8494 * @param {Object} [config] Configuration options
8495 * @cfg {Object} [menu] Configuration options to pass to menu widget
8497 OO
.ui
.InlineMenuWidget
= function OoUiInlineMenuWidget( config
) {
8498 // Configuration initialization
8499 config
= $.extend( { indicator
: 'down' }, config
);
8501 // Parent constructor
8502 OO
.ui
.InlineMenuWidget
.super.call( this, config
);
8504 // Mixin constructors
8505 OO
.ui
.IconElement
.call( this, config
);
8506 OO
.ui
.IndicatorElement
.call( this, config
);
8507 OO
.ui
.LabelElement
.call( this, config
);
8508 OO
.ui
.TitledElement
.call( this, $.extend( {}, config
, { $titled
: this.$label
} ) );
8511 this.menu
= new OO
.ui
.MenuWidget( $.extend( { $: this.$, widget
: this }, config
.menu
) );
8512 this.$handle
= this.$( '<span>' );
8515 this.$element
.on( { click
: OO
.ui
.bind( this.onClick
, this ) } );
8516 this.menu
.connect( this, { select
: 'onMenuSelect' } );
8520 .addClass( 'oo-ui-inlineMenuWidget-handle' )
8521 .append( this.$icon
, this.$label
, this.$indicator
);
8523 .addClass( 'oo-ui-inlineMenuWidget' )
8524 .append( this.$handle
, this.menu
.$element
);
8529 OO
.inheritClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.Widget
);
8530 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IconElement
);
8531 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.IndicatorElement
);
8532 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.LabelElement
);
8533 OO
.mixinClass( OO
.ui
.InlineMenuWidget
, OO
.ui
.TitledElement
);
8540 * @return {OO.ui.MenuWidget} Menu of widget
8542 OO
.ui
.InlineMenuWidget
.prototype.getMenu = function () {
8547 * Handles menu select events.
8549 * @param {OO.ui.MenuItemWidget} item Selected menu item
8551 OO
.ui
.InlineMenuWidget
.prototype.onMenuSelect = function ( item
) {
8558 selectedLabel
= item
.getLabel();
8560 // If the label is a DOM element, clone it, because setLabel will append() it
8561 if ( selectedLabel
instanceof jQuery
) {
8562 selectedLabel
= selectedLabel
.clone();
8565 this.setLabel( selectedLabel
);
8569 * Handles mouse click events.
8571 * @param {jQuery.Event} e Mouse click event
8573 OO
.ui
.InlineMenuWidget
.prototype.onClick = function ( e
) {
8574 // Skip clicks within the menu
8575 if ( $.contains( this.menu
.$element
[0], e
.target
) ) {
8579 if ( !this.isDisabled() ) {
8580 if ( this.menu
.isVisible() ) {
8581 this.menu
.toggle( false );
8583 this.menu
.toggle( true );
8590 * Base class for input widgets.
8594 * @extends OO.ui.Widget
8597 * @param {Object} [config] Configuration options
8598 * @cfg {string} [name=''] HTML input name
8599 * @cfg {string} [value=''] Input value
8600 * @cfg {boolean} [readOnly=false] Prevent changes
8601 * @cfg {Function} [inputFilter] Filter function to apply to the input. Takes a string argument and returns a string.
8603 OO
.ui
.InputWidget
= function OoUiInputWidget( config
) {
8604 // Config intialization
8605 config
= $.extend( { readOnly
: false }, config
);
8607 // Parent constructor
8608 OO
.ui
.InputWidget
.super.call( this, config
);
8611 this.$input
= this.getInputElement( config
);
8613 this.readOnly
= false;
8614 this.inputFilter
= config
.inputFilter
;
8617 this.$input
.on( 'keydown mouseup cut paste change input select', OO
.ui
.bind( this.onEdit
, this ) );
8621 .attr( 'name', config
.name
)
8622 .prop( 'disabled', this.isDisabled() );
8623 this.setReadOnly( config
.readOnly
);
8624 this.$element
.addClass( 'oo-ui-inputWidget' ).append( this.$input
);
8625 this.setValue( config
.value
);
8630 OO
.inheritClass( OO
.ui
.InputWidget
, OO
.ui
.Widget
);
8642 * Get input element.
8644 * @param {Object} [config] Configuration options
8645 * @return {jQuery} Input element
8647 OO
.ui
.InputWidget
.prototype.getInputElement = function () {
8648 return this.$( '<input>' );
8652 * Handle potentially value-changing events.
8654 * @param {jQuery.Event} e Key down, mouse up, cut, paste, change, input, or select event
8656 OO
.ui
.InputWidget
.prototype.onEdit = function () {
8658 if ( !this.isDisabled() ) {
8659 // Allow the stack to clear so the value will be updated
8660 setTimeout( function () {
8661 widget
.setValue( widget
.$input
.val() );
8667 * Get the value of the input.
8669 * @return {string} Input value
8671 OO
.ui
.InputWidget
.prototype.getValue = function () {
8676 * Sets the direction of the current input, either RTL or LTR
8678 * @param {boolean} isRTL
8680 OO
.ui
.InputWidget
.prototype.setRTL = function ( isRTL
) {
8682 this.$input
.removeClass( 'oo-ui-ltr' );
8683 this.$input
.addClass( 'oo-ui-rtl' );
8685 this.$input
.removeClass( 'oo-ui-rtl' );
8686 this.$input
.addClass( 'oo-ui-ltr' );
8691 * Set the value of the input.
8693 * @param {string} value New value
8697 OO
.ui
.InputWidget
.prototype.setValue = function ( value
) {
8698 value
= this.sanitizeValue( value
);
8699 if ( this.value
!== value
) {
8701 this.emit( 'change', this.value
);
8703 // Update the DOM if it has changed. Note that with sanitizeValue, it
8704 // is possible for the DOM value to change without this.value changing.
8705 if ( this.$input
.val() !== this.value
) {
8706 this.$input
.val( this.value
);
8712 * Sanitize incoming value.
8714 * Ensures value is a string, and converts undefined and null to empty strings.
8716 * @param {string} value Original value
8717 * @return {string} Sanitized value
8719 OO
.ui
.InputWidget
.prototype.sanitizeValue = function ( value
) {
8720 if ( value
=== undefined || value
=== null ) {
8722 } else if ( this.inputFilter
) {
8723 return this.inputFilter( String( value
) );
8725 return String( value
);
8730 * Simulate the behavior of clicking on a label bound to this input.
8732 OO
.ui
.InputWidget
.prototype.simulateLabelClick = function () {
8733 if ( !this.isDisabled() ) {
8734 if ( this.$input
.is( ':checkbox,:radio' ) ) {
8735 this.$input
.click();
8736 } else if ( this.$input
.is( ':input' ) ) {
8737 this.$input
[0].focus();
8743 * Check if the widget is read-only.
8747 OO
.ui
.InputWidget
.prototype.isReadOnly = function () {
8748 return this.readOnly
;
8752 * Set the read-only state of the widget.
8754 * This should probably change the widgets's appearance and prevent it from being used.
8756 * @param {boolean} state Make input read-only
8759 OO
.ui
.InputWidget
.prototype.setReadOnly = function ( state
) {
8760 this.readOnly
= !!state
;
8761 this.$input
.prop( 'readOnly', this.readOnly
);
8768 OO
.ui
.InputWidget
.prototype.setDisabled = function ( state
) {
8769 OO
.ui
.InputWidget
.super.prototype.setDisabled
.call( this, state
);
8770 if ( this.$input
) {
8771 this.$input
.prop( 'disabled', this.isDisabled() );
8781 OO
.ui
.InputWidget
.prototype.focus = function () {
8782 this.$input
[0].focus();
8791 OO
.ui
.InputWidget
.prototype.blur = function () {
8792 this.$input
[0].blur();
8797 * Checkbox input widget.
8800 * @extends OO.ui.InputWidget
8803 * @param {Object} [config] Configuration options
8805 OO
.ui
.CheckboxInputWidget
= function OoUiCheckboxInputWidget( config
) {
8806 // Parent constructor
8807 OO
.ui
.CheckboxInputWidget
.super.call( this, config
);
8810 this.$element
.addClass( 'oo-ui-checkboxInputWidget' );
8815 OO
.inheritClass( OO
.ui
.CheckboxInputWidget
, OO
.ui
.InputWidget
);
8822 * Get input element.
8824 * @return {jQuery} Input element
8826 OO
.ui
.CheckboxInputWidget
.prototype.getInputElement = function () {
8827 return this.$( '<input type="checkbox" />' );
8831 * Get checked state of the checkbox
8833 * @return {boolean} If the checkbox is checked
8835 OO
.ui
.CheckboxInputWidget
.prototype.getValue = function () {
8842 OO
.ui
.CheckboxInputWidget
.prototype.setValue = function ( value
) {
8844 if ( this.value
!== value
) {
8846 this.$input
.prop( 'checked', this.value
);
8847 this.emit( 'change', this.value
);
8854 OO
.ui
.CheckboxInputWidget
.prototype.onEdit = function () {
8856 if ( !this.isDisabled() ) {
8857 // Allow the stack to clear so the value will be updated
8858 setTimeout( function () {
8859 widget
.setValue( widget
.$input
.prop( 'checked' ) );
8865 * Input widget with a text field.
8868 * @extends OO.ui.InputWidget
8869 * @mixins OO.ui.IconElement
8870 * @mixins OO.ui.IndicatorElement
8873 * @param {Object} [config] Configuration options
8874 * @cfg {string} [placeholder] Placeholder text
8875 * @cfg {boolean} [multiline=false] Allow multiple lines of text
8876 * @cfg {boolean} [autosize=false] Automatically resize to fit content
8877 * @cfg {boolean} [maxRows=10] Maximum number of rows to make visible when autosizing
8879 OO
.ui
.TextInputWidget
= function OoUiTextInputWidget( config
) {
8880 // Configuration initialization
8881 config
= config
|| {};
8883 // Parent constructor
8884 OO
.ui
.TextInputWidget
.super.call( this, config
);
8886 // Mixin constructors
8887 OO
.ui
.IconElement
.call( this, config
);
8888 OO
.ui
.IndicatorElement
.call( this, config
);
8892 this.multiline
= !!config
.multiline
;
8893 this.autosize
= !!config
.autosize
;
8894 this.maxRows
= config
.maxRows
!== undefined ? config
.maxRows
: 10;
8897 this.$input
.on( 'keypress', OO
.ui
.bind( this.onKeyPress
, this ) );
8898 this.$element
.on( 'DOMNodeInsertedIntoDocument', OO
.ui
.bind( this.onElementAttach
, this ) );
8899 this.$icon
.on( 'mousedown', OO
.ui
.bind( this.onIconMouseDown
, this ) );
8900 this.$indicator
.on( 'mousedown', OO
.ui
.bind( this.onIndicatorMouseDown
, this ) );
8904 .addClass( 'oo-ui-textInputWidget' )
8905 .append( this.$icon
, this.$indicator
);
8906 if ( config
.placeholder
) {
8907 this.$input
.attr( 'placeholder', config
.placeholder
);
8909 this.$element
.attr( 'role', 'textbox' );
8914 OO
.inheritClass( OO
.ui
.TextInputWidget
, OO
.ui
.InputWidget
);
8915 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IconElement
);
8916 OO
.mixinClass( OO
.ui
.TextInputWidget
, OO
.ui
.IndicatorElement
);
8921 * User presses enter inside the text box.
8923 * Not called if input is multiline.
8929 * User clicks the icon.
8935 * User clicks the indicator.
8943 * Handle icon mouse down events.
8945 * @param {jQuery.Event} e Mouse down event
8948 OO
.ui
.TextInputWidget
.prototype.onIconMouseDown = function ( e
) {
8949 if ( e
.which
=== 1 ) {
8950 this.$input
[0].focus();
8951 this.emit( 'icon' );
8957 * Handle indicator mouse down events.
8959 * @param {jQuery.Event} e Mouse down event
8962 OO
.ui
.TextInputWidget
.prototype.onIndicatorMouseDown = function ( e
) {
8963 if ( e
.which
=== 1 ) {
8964 this.$input
[0].focus();
8965 this.emit( 'indicator' );
8971 * Handle key press events.
8973 * @param {jQuery.Event} e Key press event
8974 * @fires enter If enter key is pressed and input is not multiline
8976 OO
.ui
.TextInputWidget
.prototype.onKeyPress = function ( e
) {
8977 if ( e
.which
=== OO
.ui
.Keys
.ENTER
&& !this.multiline
) {
8978 this.emit( 'enter' );
8983 * Handle element attach events.
8985 * @param {jQuery.Event} e Element attach event
8987 OO
.ui
.TextInputWidget
.prototype.onElementAttach = function () {
8994 OO
.ui
.TextInputWidget
.prototype.onEdit = function () {
8998 return OO
.ui
.TextInputWidget
.super.prototype.onEdit
.call( this );
9004 OO
.ui
.TextInputWidget
.prototype.setValue = function ( value
) {
9006 OO
.ui
.TextInputWidget
.super.prototype.setValue
.call( this, value
);
9013 * Automatically adjust the size of the text input.
9015 * This only affects multi-line inputs that are auto-sized.
9019 OO
.ui
.TextInputWidget
.prototype.adjustSize = function () {
9020 var $clone
, scrollHeight
, innerHeight
, outerHeight
, maxInnerHeight
, idealHeight
;
9022 if ( this.multiline
&& this.autosize
) {
9023 $clone
= this.$input
.clone()
9024 .val( this.$input
.val() )
9025 .css( { height
: 0 } )
9026 .insertAfter( this.$input
);
9027 // Set inline height property to 0 to measure scroll height
9028 scrollHeight
= $clone
[0].scrollHeight
;
9029 // Remove inline height property to measure natural heights
9030 $clone
.css( 'height', '' );
9031 innerHeight
= $clone
.innerHeight();
9032 outerHeight
= $clone
.outerHeight();
9033 // Measure max rows height
9034 $clone
.attr( 'rows', this.maxRows
).css( 'height', 'auto' );
9035 maxInnerHeight
= $clone
.innerHeight();
9036 $clone
.removeAttr( 'rows' ).css( 'height', '' );
9038 idealHeight
= Math
.min( maxInnerHeight
, scrollHeight
);
9039 // Only apply inline height when expansion beyond natural height is needed
9042 // Use the difference between the inner and outer height as a buffer
9043 idealHeight
> outerHeight
? idealHeight
+ ( outerHeight
- innerHeight
) : ''
9050 * Get input element.
9052 * @param {Object} [config] Configuration options
9053 * @return {jQuery} Input element
9055 OO
.ui
.TextInputWidget
.prototype.getInputElement = function ( config
) {
9056 return config
.multiline
? this.$( '<textarea>' ) : this.$( '<input type="text" />' );
9062 * Check if input supports multiple lines.
9066 OO
.ui
.TextInputWidget
.prototype.isMultiline = function () {
9067 return !!this.multiline
;
9071 * Check if input automatically adjusts its size.
9075 OO
.ui
.TextInputWidget
.prototype.isAutosizing = function () {
9076 return !!this.autosize
;
9080 * Check if input is pending.
9084 OO
.ui
.TextInputWidget
.prototype.isPending = function () {
9085 return !!this.pending
;
9089 * Increase the pending stack.
9093 OO
.ui
.TextInputWidget
.prototype.pushPending = function () {
9094 if ( this.pending
=== 0 ) {
9095 this.$element
.addClass( 'oo-ui-textInputWidget-pending' );
9096 this.$input
.addClass( 'oo-ui-texture-pending' );
9104 * Reduce the pending stack.
9110 OO
.ui
.TextInputWidget
.prototype.popPending = function () {
9111 if ( this.pending
=== 1 ) {
9112 this.$element
.removeClass( 'oo-ui-textInputWidget-pending' );
9113 this.$input
.removeClass( 'oo-ui-texture-pending' );
9115 this.pending
= Math
.max( 0, this.pending
- 1 );
9121 * Select the contents of the input.
9125 OO
.ui
.TextInputWidget
.prototype.select = function () {
9126 this.$input
.select();
9131 * Text input with a menu of optional values.
9134 * @extends OO.ui.Widget
9137 * @param {Object} [config] Configuration options
9138 * @cfg {Object} [menu] Configuration options to pass to menu widget
9139 * @cfg {Object} [input] Configuration options to pass to input widget
9141 OO
.ui
.ComboBoxWidget
= function OoUiComboBoxWidget( config
) {
9142 // Configuration initialization
9143 config
= config
|| {};
9145 // Parent constructor
9146 OO
.ui
.ComboBoxWidget
.super.call( this, config
);
9149 this.input
= new OO
.ui
.TextInputWidget( $.extend(
9150 { $: this.$, indicator
: 'down', disabled
: this.isDisabled() },
9153 this.menu
= new OO
.ui
.MenuWidget( $.extend(
9154 { $: this.$, widget
: this, input
: this.input
, disabled
: this.isDisabled() },
9159 this.input
.connect( this, {
9160 change
: 'onInputChange',
9161 indicator
: 'onInputIndicator',
9162 enter
: 'onInputEnter'
9164 this.menu
.connect( this, {
9165 choose
: 'onMenuChoose',
9166 add
: 'onMenuItemsChange',
9167 remove
: 'onMenuItemsChange'
9171 this.$element
.addClass( 'oo-ui-comboBoxWidget' ).append(
9172 this.input
.$element
,
9175 this.onMenuItemsChange();
9180 OO
.inheritClass( OO
.ui
.ComboBoxWidget
, OO
.ui
.Widget
);
9185 * Handle input change events.
9187 * @param {string} value New value
9189 OO
.ui
.ComboBoxWidget
.prototype.onInputChange = function ( value
) {
9190 var match
= this.menu
.getItemFromData( value
);
9192 this.menu
.selectItem( match
);
9194 if ( !this.isDisabled() ) {
9195 this.menu
.toggle( true );
9200 * Handle input indicator events.
9202 OO
.ui
.ComboBoxWidget
.prototype.onInputIndicator = function () {
9203 if ( !this.isDisabled() ) {
9209 * Handle input enter events.
9211 OO
.ui
.ComboBoxWidget
.prototype.onInputEnter = function () {
9212 if ( !this.isDisabled() ) {
9213 this.menu
.toggle( false );
9218 * Handle menu choose events.
9220 * @param {OO.ui.OptionWidget} item Chosen item
9222 OO
.ui
.ComboBoxWidget
.prototype.onMenuChoose = function ( item
) {
9224 this.input
.setValue( item
.getData() );
9229 * Handle menu item change events.
9231 OO
.ui
.ComboBoxWidget
.prototype.onMenuItemsChange = function () {
9232 this.$element
.toggleClass( 'oo-ui-comboBoxWidget-empty', this.menu
.isEmpty() );
9238 OO
.ui
.ComboBoxWidget
.prototype.setDisabled = function ( disabled
) {
9240 OO
.ui
.ComboBoxWidget
.super.prototype.setDisabled
.call( this, disabled
);
9243 this.input
.setDisabled( this.isDisabled() );
9246 this.menu
.setDisabled( this.isDisabled() );
9256 * @extends OO.ui.Widget
9257 * @mixins OO.ui.LabelElement
9260 * @param {Object} [config] Configuration options
9262 OO
.ui
.LabelWidget
= function OoUiLabelWidget( config
) {
9263 // Config intialization
9264 config
= config
|| {};
9266 // Parent constructor
9267 OO
.ui
.LabelWidget
.super.call( this, config
);
9269 // Mixin constructors
9270 OO
.ui
.LabelElement
.call( this, $.extend( {}, config
, { $label
: this.$element
} ) );
9273 this.input
= config
.input
;
9276 if ( this.input
instanceof OO
.ui
.InputWidget
) {
9277 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
9281 this.$element
.addClass( 'oo-ui-labelWidget' );
9286 OO
.inheritClass( OO
.ui
.LabelWidget
, OO
.ui
.Widget
);
9287 OO
.mixinClass( OO
.ui
.LabelWidget
, OO
.ui
.LabelElement
);
9289 /* Static Properties */
9291 OO
.ui
.LabelWidget
.static.tagName
= 'span';
9296 * Handles label mouse click events.
9298 * @param {jQuery.Event} e Mouse click event
9300 OO
.ui
.LabelWidget
.prototype.onClick = function () {
9301 this.input
.simulateLabelClick();
9306 * Generic option widget for use with OO.ui.SelectWidget.
9309 * @extends OO.ui.Widget
9310 * @mixins OO.ui.LabelElement
9311 * @mixins OO.ui.FlaggedElement
9314 * @param {Mixed} data Option data
9315 * @param {Object} [config] Configuration options
9316 * @cfg {string} [rel] Value for `rel` attribute in DOM, allowing per-option styling
9318 OO
.ui
.OptionWidget
= function OoUiOptionWidget( data
, config
) {
9319 // Config intialization
9320 config
= config
|| {};
9322 // Parent constructor
9323 OO
.ui
.OptionWidget
.super.call( this, config
);
9325 // Mixin constructors
9326 OO
.ui
.ItemWidget
.call( this );
9327 OO
.ui
.LabelElement
.call( this, config
);
9328 OO
.ui
.FlaggedElement
.call( this, config
);
9332 this.selected
= false;
9333 this.highlighted
= false;
9334 this.pressed
= false;
9338 .data( 'oo-ui-optionWidget', this )
9339 .attr( 'rel', config
.rel
)
9340 .attr( 'role', 'option' )
9341 .addClass( 'oo-ui-optionWidget' )
9342 .append( this.$label
);
9344 .prepend( this.$icon
)
9345 .append( this.$indicator
);
9350 OO
.inheritClass( OO
.ui
.OptionWidget
, OO
.ui
.Widget
);
9351 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.ItemWidget
);
9352 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.LabelElement
);
9353 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.FlaggedElement
);
9355 /* Static Properties */
9357 OO
.ui
.OptionWidget
.static.selectable
= true;
9359 OO
.ui
.OptionWidget
.static.highlightable
= true;
9361 OO
.ui
.OptionWidget
.static.pressable
= true;
9363 OO
.ui
.OptionWidget
.static.scrollIntoViewOnSelect
= false;
9368 * Check if option can be selected.
9370 * @return {boolean} Item is selectable
9372 OO
.ui
.OptionWidget
.prototype.isSelectable = function () {
9373 return this.constructor.static.selectable
&& !this.isDisabled();
9377 * Check if option can be highlighted.
9379 * @return {boolean} Item is highlightable
9381 OO
.ui
.OptionWidget
.prototype.isHighlightable = function () {
9382 return this.constructor.static.highlightable
&& !this.isDisabled();
9386 * Check if option can be pressed.
9388 * @return {boolean} Item is pressable
9390 OO
.ui
.OptionWidget
.prototype.isPressable = function () {
9391 return this.constructor.static.pressable
&& !this.isDisabled();
9395 * Check if option is selected.
9397 * @return {boolean} Item is selected
9399 OO
.ui
.OptionWidget
.prototype.isSelected = function () {
9400 return this.selected
;
9404 * Check if option is highlighted.
9406 * @return {boolean} Item is highlighted
9408 OO
.ui
.OptionWidget
.prototype.isHighlighted = function () {
9409 return this.highlighted
;
9413 * Check if option is pressed.
9415 * @return {boolean} Item is pressed
9417 OO
.ui
.OptionWidget
.prototype.isPressed = function () {
9418 return this.pressed
;
9422 * Set selected state.
9424 * @param {boolean} [state=false] Select option
9427 OO
.ui
.OptionWidget
.prototype.setSelected = function ( state
) {
9428 if ( this.constructor.static.selectable
) {
9429 this.selected
= !!state
;
9430 this.$element
.toggleClass( 'oo-ui-optionWidget-selected', state
);
9431 if ( state
&& this.constructor.static.scrollIntoViewOnSelect
) {
9432 this.scrollElementIntoView();
9439 * Set highlighted state.
9441 * @param {boolean} [state=false] Highlight option
9444 OO
.ui
.OptionWidget
.prototype.setHighlighted = function ( state
) {
9445 if ( this.constructor.static.highlightable
) {
9446 this.highlighted
= !!state
;
9447 this.$element
.toggleClass( 'oo-ui-optionWidget-highlighted', state
);
9453 * Set pressed state.
9455 * @param {boolean} [state=false] Press option
9458 OO
.ui
.OptionWidget
.prototype.setPressed = function ( state
) {
9459 if ( this.constructor.static.pressable
) {
9460 this.pressed
= !!state
;
9461 this.$element
.toggleClass( 'oo-ui-optionWidget-pressed', state
);
9467 * Make the option's highlight flash.
9469 * While flashing, the visual style of the pressed state is removed if present.
9471 * @return {jQuery.Promise} Promise resolved when flashing is done
9473 OO
.ui
.OptionWidget
.prototype.flash = function () {
9475 $element
= this.$element
,
9476 deferred
= $.Deferred();
9478 if ( !this.isDisabled() && this.constructor.static.pressable
) {
9479 $element
.removeClass( 'oo-ui-optionWidget-highlighted oo-ui-optionWidget-pressed' );
9480 setTimeout( function () {
9481 // Restore original classes
9483 .toggleClass( 'oo-ui-optionWidget-highlighted', widget
.highlighted
)
9484 .toggleClass( 'oo-ui-optionWidget-pressed', widget
.pressed
);
9486 setTimeout( function () {
9493 return deferred
.promise();
9499 * @return {Mixed} Option data
9501 OO
.ui
.OptionWidget
.prototype.getData = function () {
9506 * Option widget with an option icon and indicator.
9508 * Use together with OO.ui.SelectWidget.
9511 * @extends OO.ui.OptionWidget
9512 * @mixins OO.ui.IconElement
9513 * @mixins OO.ui.IndicatorElement
9516 * @param {Mixed} data Option data
9517 * @param {Object} [config] Configuration options
9519 OO
.ui
.DecoratedOptionWidget
= function OoUiDecoratedOptionWidget( data
, config
) {
9520 // Parent constructor
9521 OO
.ui
.DecoratedOptionWidget
.super.call( this, data
, config
);
9523 // Mixin constructors
9524 OO
.ui
.IconElement
.call( this, config
);
9525 OO
.ui
.IndicatorElement
.call( this, config
);
9529 .addClass( 'oo-ui-decoratedOptionWidget' )
9530 .prepend( this.$icon
)
9531 .append( this.$indicator
);
9536 OO
.inheritClass( OO
.ui
.DecoratedOptionWidget
, OO
.ui
.OptionWidget
);
9537 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IconElement
);
9538 OO
.mixinClass( OO
.ui
.OptionWidget
, OO
.ui
.IndicatorElement
);
9541 * Option widget that looks like a button.
9543 * Use together with OO.ui.ButtonSelectWidget.
9546 * @extends OO.ui.DecoratedOptionWidget
9547 * @mixins OO.ui.ButtonElement
9550 * @param {Mixed} data Option data
9551 * @param {Object} [config] Configuration options
9553 OO
.ui
.ButtonOptionWidget
= function OoUiButtonOptionWidget( data
, config
) {
9554 // Parent constructor
9555 OO
.ui
.ButtonOptionWidget
.super.call( this, data
, config
);
9557 // Mixin constructors
9558 OO
.ui
.ButtonElement
.call( this, config
);
9561 this.$element
.addClass( 'oo-ui-buttonOptionWidget' );
9562 this.$button
.append( this.$element
.contents() );
9563 this.$element
.append( this.$button
);
9568 OO
.inheritClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.DecoratedOptionWidget
);
9569 OO
.mixinClass( OO
.ui
.ButtonOptionWidget
, OO
.ui
.ButtonElement
);
9571 /* Static Properties */
9573 // Allow button mouse down events to pass through so they can be handled by the parent select widget
9574 OO
.ui
.ButtonOptionWidget
.static.cancelButtonMouseDownEvents
= false;
9581 OO
.ui
.ButtonOptionWidget
.prototype.setSelected = function ( state
) {
9582 OO
.ui
.ButtonOptionWidget
.super.prototype.setSelected
.call( this, state
);
9584 if ( this.constructor.static.selectable
) {
9585 this.setActive( state
);
9592 * Item of an OO.ui.MenuWidget.
9595 * @extends OO.ui.DecoratedOptionWidget
9598 * @param {Mixed} data Item data
9599 * @param {Object} [config] Configuration options
9601 OO
.ui
.MenuItemWidget
= function OoUiMenuItemWidget( data
, config
) {
9602 // Configuration initialization
9603 config
= $.extend( { icon
: 'check' }, config
);
9605 // Parent constructor
9606 OO
.ui
.MenuItemWidget
.super.call( this, data
, config
);
9610 .attr( 'role', 'menuitem' )
9611 .addClass( 'oo-ui-menuItemWidget' );
9616 OO
.inheritClass( OO
.ui
.MenuItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9619 * Section to group one or more items in a OO.ui.MenuWidget.
9622 * @extends OO.ui.DecoratedOptionWidget
9625 * @param {Mixed} data Item data
9626 * @param {Object} [config] Configuration options
9628 OO
.ui
.MenuSectionItemWidget
= function OoUiMenuSectionItemWidget( data
, config
) {
9629 // Parent constructor
9630 OO
.ui
.MenuSectionItemWidget
.super.call( this, data
, config
);
9633 this.$element
.addClass( 'oo-ui-menuSectionItemWidget' );
9638 OO
.inheritClass( OO
.ui
.MenuSectionItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9640 /* Static Properties */
9642 OO
.ui
.MenuSectionItemWidget
.static.selectable
= false;
9644 OO
.ui
.MenuSectionItemWidget
.static.highlightable
= false;
9647 * Items for an OO.ui.OutlineWidget.
9650 * @extends OO.ui.DecoratedOptionWidget
9653 * @param {Mixed} data Item data
9654 * @param {Object} [config] Configuration options
9655 * @cfg {number} [level] Indentation level
9656 * @cfg {boolean} [movable] Allow modification from outline controls
9658 OO
.ui
.OutlineItemWidget
= function OoUiOutlineItemWidget( data
, config
) {
9659 // Config intialization
9660 config
= config
|| {};
9662 // Parent constructor
9663 OO
.ui
.OutlineItemWidget
.super.call( this, data
, config
);
9667 this.movable
= !!config
.movable
;
9668 this.removable
= !!config
.removable
;
9671 this.$element
.addClass( 'oo-ui-outlineItemWidget' );
9672 this.setLevel( config
.level
);
9677 OO
.inheritClass( OO
.ui
.OutlineItemWidget
, OO
.ui
.DecoratedOptionWidget
);
9679 /* Static Properties */
9681 OO
.ui
.OutlineItemWidget
.static.highlightable
= false;
9683 OO
.ui
.OutlineItemWidget
.static.scrollIntoViewOnSelect
= true;
9685 OO
.ui
.OutlineItemWidget
.static.levelClass
= 'oo-ui-outlineItemWidget-level-';
9687 OO
.ui
.OutlineItemWidget
.static.levels
= 3;
9692 * Check if item is movable.
9694 * Movablilty is used by outline controls.
9696 * @return {boolean} Item is movable
9698 OO
.ui
.OutlineItemWidget
.prototype.isMovable = function () {
9699 return this.movable
;
9703 * Check if item is removable.
9705 * Removablilty is used by outline controls.
9707 * @return {boolean} Item is removable
9709 OO
.ui
.OutlineItemWidget
.prototype.isRemovable = function () {
9710 return this.removable
;
9714 * Get indentation level.
9716 * @return {number} Indentation level
9718 OO
.ui
.OutlineItemWidget
.prototype.getLevel = function () {
9725 * Movablilty is used by outline controls.
9727 * @param {boolean} movable Item is movable
9730 OO
.ui
.OutlineItemWidget
.prototype.setMovable = function ( movable
) {
9731 this.movable
= !!movable
;
9738 * Removablilty is used by outline controls.
9740 * @param {boolean} movable Item is removable
9743 OO
.ui
.OutlineItemWidget
.prototype.setRemovable = function ( removable
) {
9744 this.removable
= !!removable
;
9749 * Set indentation level.
9751 * @param {number} [level=0] Indentation level, in the range of [0,#maxLevel]
9754 OO
.ui
.OutlineItemWidget
.prototype.setLevel = function ( level
) {
9755 var levels
= this.constructor.static.levels
,
9756 levelClass
= this.constructor.static.levelClass
,
9759 this.level
= level
? Math
.max( 0, Math
.min( levels
- 1, level
) ) : 0;
9761 if ( this.level
=== i
) {
9762 this.$element
.addClass( levelClass
+ i
);
9764 this.$element
.removeClass( levelClass
+ i
);
9772 * Container for content that is overlaid and positioned absolutely.
9775 * @extends OO.ui.Widget
9776 * @mixins OO.ui.LabelElement
9779 * @param {Object} [config] Configuration options
9780 * @cfg {number} [width=320] Width of popup in pixels
9781 * @cfg {number} [height] Height of popup, omit to use automatic height
9782 * @cfg {boolean} [anchor=true] Show anchor pointing to origin of popup
9783 * @cfg {string} [align='center'] Alignment of popup to origin
9784 * @cfg {jQuery} [$container] Container to prevent popup from rendering outside of
9785 * @cfg {jQuery} [$content] Content to append to the popup's body
9786 * @cfg {boolean} [autoClose=false] Popup auto-closes when it loses focus
9787 * @cfg {jQuery} [$autoCloseIgnore] Elements to not auto close when clicked
9788 * @cfg {boolean} [head] Show label and close button at the top
9789 * @cfg {boolean} [padded] Add padding to the body
9791 OO
.ui
.PopupWidget
= function OoUiPopupWidget( config
) {
9792 // Config intialization
9793 config
= config
|| {};
9795 // Parent constructor
9796 OO
.ui
.PopupWidget
.super.call( this, config
);
9798 // Mixin constructors
9799 OO
.ui
.LabelElement
.call( this, config
);
9800 OO
.ui
.ClippableElement
.call( this, config
);
9803 this.visible
= false;
9804 this.$popup
= this.$( '<div>' );
9805 this.$head
= this.$( '<div>' );
9806 this.$body
= this.$( '<div>' );
9807 this.$anchor
= this.$( '<div>' );
9808 this.$container
= config
.$container
|| this.$( 'body' );
9809 this.autoClose
= !!config
.autoClose
;
9810 this.$autoCloseIgnore
= config
.$autoCloseIgnore
;
9811 this.transitionTimeout
= null;
9813 this.width
= config
.width
!== undefined ? config
.width
: 320;
9814 this.height
= config
.height
!== undefined ? config
.height
: null;
9815 this.align
= config
.align
|| 'center';
9816 this.closeButton
= new OO
.ui
.ButtonWidget( { $: this.$, framed
: false, icon
: 'close' } );
9817 this.onMouseDownHandler
= OO
.ui
.bind( this.onMouseDown
, this );
9820 this.closeButton
.connect( this, { click
: 'onCloseButtonClick' } );
9823 this.toggleAnchor( config
.anchor
=== undefined || config
.anchor
);
9824 this.$body
.addClass( 'oo-ui-popupWidget-body' );
9825 this.$anchor
.addClass( 'oo-ui-popupWidget-anchor' );
9827 .addClass( 'oo-ui-popupWidget-head' )
9828 .append( this.$label
, this.closeButton
.$element
);
9829 if ( !config
.head
) {
9833 .addClass( 'oo-ui-popupWidget-popup' )
9834 .append( this.$head
, this.$body
);
9837 .addClass( 'oo-ui-popupWidget' )
9838 .append( this.$popup
, this.$anchor
);
9839 // Move content, which was added to #$element by OO.ui.Widget, to the body
9840 if ( config
.$content
instanceof jQuery
) {
9841 this.$body
.append( config
.$content
);
9843 if ( config
.padded
) {
9844 this.$body
.addClass( 'oo-ui-popupWidget-body-padded' );
9846 this.setClippableElement( this.$body
);
9851 OO
.inheritClass( OO
.ui
.PopupWidget
, OO
.ui
.Widget
);
9852 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.LabelElement
);
9853 OO
.mixinClass( OO
.ui
.PopupWidget
, OO
.ui
.ClippableElement
);
9868 * Handles mouse down events.
9870 * @param {jQuery.Event} e Mouse down event
9872 OO
.ui
.PopupWidget
.prototype.onMouseDown = function ( e
) {
9875 !$.contains( this.$element
[0], e
.target
) &&
9876 ( !this.$autoCloseIgnore
|| !this.$autoCloseIgnore
.has( e
.target
).length
)
9878 this.toggle( false );
9883 * Bind mouse down listener.
9885 OO
.ui
.PopupWidget
.prototype.bindMouseDownListener = function () {
9886 // Capture clicks outside popup
9887 this.getElementWindow().addEventListener( 'mousedown', this.onMouseDownHandler
, true );
9891 * Handles close button click events.
9893 OO
.ui
.PopupWidget
.prototype.onCloseButtonClick = function () {
9894 if ( this.isVisible() ) {
9895 this.toggle( false );
9900 * Unbind mouse down listener.
9902 OO
.ui
.PopupWidget
.prototype.unbindMouseDownListener = function () {
9903 this.getElementWindow().removeEventListener( 'mousedown', this.onMouseDownHandler
, true );
9907 * Set whether to show a anchor.
9909 * @param {boolean} [show] Show anchor, omit to toggle
9911 OO
.ui
.PopupWidget
.prototype.toggleAnchor = function ( show
) {
9912 show
= show
=== undefined ? !this.anchored
: !!show
;
9914 if ( this.anchored
!== show
) {
9916 this.$element
.addClass( 'oo-ui-popupWidget-anchored' );
9918 this.$element
.removeClass( 'oo-ui-popupWidget-anchored' );
9920 this.anchored
= show
;
9925 * Check if showing a anchor.
9927 * @return {boolean} anchor is visible
9929 OO
.ui
.PopupWidget
.prototype.hasAnchor = function () {
9936 OO
.ui
.PopupWidget
.prototype.toggle = function ( show
) {
9937 show
= show
=== undefined ? !this.isVisible() : !!show
;
9939 var change
= show
!== this.isVisible();
9942 OO
.ui
.PopupWidget
.super.prototype.toggle
.call( this, show
);
9946 if ( this.autoClose
) {
9947 this.bindMouseDownListener();
9949 this.updateDimensions();
9950 this.toggleClipping( true );
9952 this.toggleClipping( false );
9953 if ( this.autoClose
) {
9954 this.unbindMouseDownListener();
9963 * Set the size of the popup.
9965 * Changing the size may also change the popup's position depending on the alignment.
9967 * @param {number} width Width
9968 * @param {number} height Height
9969 * @param {boolean} [transition=false] Use a smooth transition
9972 OO
.ui
.PopupWidget
.prototype.setSize = function ( width
, height
, transition
) {
9974 this.height
= height
!== undefined ? height
: null;
9975 if ( this.isVisible() ) {
9976 this.updateDimensions( transition
);
9981 * Update the size and position.
9983 * Only use this to keep the popup properly anchored. Use #setSize to change the size, and this will
9984 * be called automatically.
9986 * @param {boolean} [transition=false] Use a smooth transition
9989 OO
.ui
.PopupWidget
.prototype.updateDimensions = function ( transition
) {
9992 originOffset
= Math
.round( this.$element
.offset().left
),
9993 containerLeft
= Math
.round( this.$container
.offset().left
),
9994 containerWidth
= this.$container
.innerWidth(),
9995 containerRight
= containerLeft
+ containerWidth
,
9996 popupOffset
= this.width
* ( { left
: 0, center
: -0.5, right
: -1 } )[this.align
],
9997 anchorWidth
= this.$anchor
.width(),
9998 popupLeft
= popupOffset
- padding
,
9999 popupRight
= popupOffset
+ padding
+ this.width
+ padding
,
10000 overlapLeft
= ( originOffset
+ popupLeft
) - containerLeft
,
10001 overlapRight
= containerRight
- ( originOffset
+ popupRight
);
10003 // Prevent transition from being interrupted
10004 clearTimeout( this.transitionTimeout
);
10005 if ( transition
) {
10006 // Enable transition
10007 this.$element
.addClass( 'oo-ui-popupWidget-transitioning' );
10010 if ( overlapRight
< 0 ) {
10011 popupOffset
+= overlapRight
;
10012 } else if ( overlapLeft
< 0 ) {
10013 popupOffset
-= overlapLeft
;
10016 // Adjust offset to avoid anchor being rendered too close to the edge
10017 if ( this.align
=== 'right' ) {
10018 popupOffset
+= anchorWidth
;
10019 } else if ( this.align
=== 'left' ) {
10020 popupOffset
-= anchorWidth
;
10023 // Position body relative to anchor and resize
10027 height
: this.height
!== null ? this.height
: 'auto'
10030 if ( transition
) {
10031 // Prevent transitioning after transition is complete
10032 this.transitionTimeout
= setTimeout( function () {
10033 widget
.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10036 // Prevent transitioning immediately
10037 this.$element
.removeClass( 'oo-ui-popupWidget-transitioning' );
10046 * Search widgets combine a query input, placed above, and a results selection widget, placed below.
10047 * Results are cleared and populated each time the query is changed.
10050 * @extends OO.ui.Widget
10053 * @param {Object} [config] Configuration options
10054 * @cfg {string|jQuery} [placeholder] Placeholder text for query input
10055 * @cfg {string} [value] Initial query value
10057 OO
.ui
.SearchWidget
= function OoUiSearchWidget( config
) {
10058 // Configuration intialization
10059 config
= config
|| {};
10061 // Parent constructor
10062 OO
.ui
.SearchWidget
.super.call( this, config
);
10065 this.query
= new OO
.ui
.TextInputWidget( {
10068 placeholder
: config
.placeholder
,
10069 value
: config
.value
10071 this.results
= new OO
.ui
.SelectWidget( { $: this.$ } );
10072 this.$query
= this.$( '<div>' );
10073 this.$results
= this.$( '<div>' );
10076 this.query
.connect( this, {
10077 change
: 'onQueryChange',
10078 enter
: 'onQueryEnter'
10080 this.results
.connect( this, {
10081 highlight
: 'onResultsHighlight',
10082 select
: 'onResultsSelect'
10084 this.query
.$input
.on( 'keydown', OO
.ui
.bind( this.onQueryKeydown
, this ) );
10088 .addClass( 'oo-ui-searchWidget-query' )
10089 .append( this.query
.$element
);
10091 .addClass( 'oo-ui-searchWidget-results' )
10092 .append( this.results
.$element
);
10094 .addClass( 'oo-ui-searchWidget' )
10095 .append( this.$results
, this.$query
);
10100 OO
.inheritClass( OO
.ui
.SearchWidget
, OO
.ui
.Widget
);
10106 * @param {Object|null} item Item data or null if no item is highlighted
10111 * @param {Object|null} item Item data or null if no item is selected
10117 * Handle query key down events.
10119 * @param {jQuery.Event} e Key down event
10121 OO
.ui
.SearchWidget
.prototype.onQueryKeydown = function ( e
) {
10122 var highlightedItem
, nextItem
,
10123 dir
= e
.which
=== OO
.ui
.Keys
.DOWN
? 1 : ( e
.which
=== OO
.ui
.Keys
.UP
? -1 : 0 );
10126 highlightedItem
= this.results
.getHighlightedItem();
10127 if ( !highlightedItem
) {
10128 highlightedItem
= this.results
.getSelectedItem();
10130 nextItem
= this.results
.getRelativeSelectableItem( highlightedItem
, dir
);
10131 this.results
.highlightItem( nextItem
);
10132 nextItem
.scrollElementIntoView();
10137 * Handle select widget select events.
10139 * Clears existing results. Subclasses should repopulate items according to new query.
10141 * @param {string} value New value
10143 OO
.ui
.SearchWidget
.prototype.onQueryChange = function () {
10145 this.results
.clearItems();
10149 * Handle select widget enter key events.
10151 * Selects highlighted item.
10153 * @param {string} value New value
10155 OO
.ui
.SearchWidget
.prototype.onQueryEnter = function () {
10157 this.results
.selectItem( this.results
.getHighlightedItem() );
10161 * Handle select widget highlight events.
10163 * @param {OO.ui.OptionWidget} item Highlighted item
10166 OO
.ui
.SearchWidget
.prototype.onResultsHighlight = function ( item
) {
10167 this.emit( 'highlight', item
? item
.getData() : null );
10171 * Handle select widget select events.
10173 * @param {OO.ui.OptionWidget} item Selected item
10176 OO
.ui
.SearchWidget
.prototype.onResultsSelect = function ( item
) {
10177 this.emit( 'select', item
? item
.getData() : null );
10181 * Get the query input.
10183 * @return {OO.ui.TextInputWidget} Query input
10185 OO
.ui
.SearchWidget
.prototype.getQuery = function () {
10190 * Get the results list.
10192 * @return {OO.ui.SelectWidget} Select list
10194 OO
.ui
.SearchWidget
.prototype.getResults = function () {
10195 return this.results
;
10199 * Generic selection of options.
10201 * Items can contain any rendering, and are uniquely identified by a has of thier data. Any widget
10202 * that provides options, from which the user must choose one, should be built on this class.
10204 * Use together with OO.ui.OptionWidget.
10207 * @extends OO.ui.Widget
10208 * @mixins OO.ui.GroupElement
10211 * @param {Object} [config] Configuration options
10212 * @cfg {OO.ui.OptionWidget[]} [items] Options to add
10214 OO
.ui
.SelectWidget
= function OoUiSelectWidget( config
) {
10215 // Config intialization
10216 config
= config
|| {};
10218 // Parent constructor
10219 OO
.ui
.SelectWidget
.super.call( this, config
);
10221 // Mixin constructors
10222 OO
.ui
.GroupWidget
.call( this, $.extend( {}, config
, { $group
: this.$element
} ) );
10225 this.pressed
= false;
10226 this.selecting
= null;
10228 this.onMouseUpHandler
= OO
.ui
.bind( this.onMouseUp
, this );
10229 this.onMouseMoveHandler
= OO
.ui
.bind( this.onMouseMove
, this );
10232 this.$element
.on( {
10233 mousedown
: OO
.ui
.bind( this.onMouseDown
, this ),
10234 mouseover
: OO
.ui
.bind( this.onMouseOver
, this ),
10235 mouseleave
: OO
.ui
.bind( this.onMouseLeave
, this )
10239 this.$element
.addClass( 'oo-ui-selectWidget oo-ui-selectWidget-depressed' );
10240 if ( $.isArray( config
.items
) ) {
10241 this.addItems( config
.items
);
10247 OO
.inheritClass( OO
.ui
.SelectWidget
, OO
.ui
.Widget
);
10249 // Need to mixin base class as well
10250 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupElement
);
10251 OO
.mixinClass( OO
.ui
.SelectWidget
, OO
.ui
.GroupWidget
);
10257 * @param {OO.ui.OptionWidget|null} item Highlighted item
10262 * @param {OO.ui.OptionWidget|null} item Pressed item
10267 * @param {OO.ui.OptionWidget|null} item Selected item
10272 * @param {OO.ui.OptionWidget|null} item Chosen item
10277 * @param {OO.ui.OptionWidget[]} items Added items
10278 * @param {number} index Index items were added at
10283 * @param {OO.ui.OptionWidget[]} items Removed items
10289 * Handle mouse down events.
10292 * @param {jQuery.Event} e Mouse down event
10294 OO
.ui
.SelectWidget
.prototype.onMouseDown = function ( e
) {
10297 if ( !this.isDisabled() && e
.which
=== 1 ) {
10298 this.togglePressed( true );
10299 item
= this.getTargetItem( e
);
10300 if ( item
&& item
.isSelectable() ) {
10301 this.pressItem( item
);
10302 this.selecting
= item
;
10303 this.getElementDocument().addEventListener(
10305 this.onMouseUpHandler
,
10308 this.getElementDocument().addEventListener(
10310 this.onMouseMoveHandler
,
10319 * Handle mouse up events.
10322 * @param {jQuery.Event} e Mouse up event
10324 OO
.ui
.SelectWidget
.prototype.onMouseUp = function ( e
) {
10327 this.togglePressed( false );
10328 if ( !this.selecting
) {
10329 item
= this.getTargetItem( e
);
10330 if ( item
&& item
.isSelectable() ) {
10331 this.selecting
= item
;
10334 if ( !this.isDisabled() && e
.which
=== 1 && this.selecting
) {
10335 this.pressItem( null );
10336 this.chooseItem( this.selecting
);
10337 this.selecting
= null;
10340 this.getElementDocument().removeEventListener(
10342 this.onMouseUpHandler
,
10345 this.getElementDocument().removeEventListener(
10347 this.onMouseMoveHandler
,
10355 * Handle mouse move events.
10358 * @param {jQuery.Event} e Mouse move event
10360 OO
.ui
.SelectWidget
.prototype.onMouseMove = function ( e
) {
10363 if ( !this.isDisabled() && this.pressed
) {
10364 item
= this.getTargetItem( e
);
10365 if ( item
&& item
!== this.selecting
&& item
.isSelectable() ) {
10366 this.pressItem( item
);
10367 this.selecting
= item
;
10374 * Handle mouse over events.
10377 * @param {jQuery.Event} e Mouse over event
10379 OO
.ui
.SelectWidget
.prototype.onMouseOver = function ( e
) {
10382 if ( !this.isDisabled() ) {
10383 item
= this.getTargetItem( e
);
10384 this.highlightItem( item
&& item
.isHighlightable() ? item
: null );
10390 * Handle mouse leave events.
10393 * @param {jQuery.Event} e Mouse over event
10395 OO
.ui
.SelectWidget
.prototype.onMouseLeave = function () {
10396 if ( !this.isDisabled() ) {
10397 this.highlightItem( null );
10403 * Get the closest item to a jQuery.Event.
10406 * @param {jQuery.Event} e
10407 * @return {OO.ui.OptionWidget|null} Outline item widget, `null` if none was found
10409 OO
.ui
.SelectWidget
.prototype.getTargetItem = function ( e
) {
10410 var $item
= this.$( e
.target
).closest( '.oo-ui-optionWidget' );
10411 if ( $item
.length
) {
10412 return $item
.data( 'oo-ui-optionWidget' );
10418 * Get selected item.
10420 * @return {OO.ui.OptionWidget|null} Selected item, `null` if no item is selected
10422 OO
.ui
.SelectWidget
.prototype.getSelectedItem = function () {
10425 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10426 if ( this.items
[i
].isSelected() ) {
10427 return this.items
[i
];
10434 * Get highlighted item.
10436 * @return {OO.ui.OptionWidget|null} Highlighted item, `null` if no item is highlighted
10438 OO
.ui
.SelectWidget
.prototype.getHighlightedItem = function () {
10441 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10442 if ( this.items
[i
].isHighlighted() ) {
10443 return this.items
[i
];
10450 * Get an existing item with equivilant data.
10452 * @param {Object} data Item data to search for
10453 * @return {OO.ui.OptionWidget|null} Item with equivilent value, `null` if none exists
10455 OO
.ui
.SelectWidget
.prototype.getItemFromData = function ( data
) {
10456 var hash
= OO
.getHash( data
);
10458 if ( hash
in this.hashes
) {
10459 return this.hashes
[hash
];
10466 * Toggle pressed state.
10468 * @param {boolean} pressed An option is being pressed
10470 OO
.ui
.SelectWidget
.prototype.togglePressed = function ( pressed
) {
10471 if ( pressed
=== undefined ) {
10472 pressed
= !this.pressed
;
10474 if ( pressed
!== this.pressed
) {
10476 .toggleClass( 'oo-ui-selectWidget-pressed', pressed
)
10477 .toggleClass( 'oo-ui-selectWidget-depressed', !pressed
);
10478 this.pressed
= pressed
;
10483 * Highlight an item.
10485 * Highlighting is mutually exclusive.
10487 * @param {OO.ui.OptionWidget} [item] Item to highlight, omit to deselect all
10491 OO
.ui
.SelectWidget
.prototype.highlightItem = function ( item
) {
10492 var i
, len
, highlighted
,
10495 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10496 highlighted
= this.items
[i
] === item
;
10497 if ( this.items
[i
].isHighlighted() !== highlighted
) {
10498 this.items
[i
].setHighlighted( highlighted
);
10503 this.emit( 'highlight', item
);
10512 * @param {OO.ui.OptionWidget} [item] Item to select, omit to deselect all
10516 OO
.ui
.SelectWidget
.prototype.selectItem = function ( item
) {
10517 var i
, len
, selected
,
10520 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10521 selected
= this.items
[i
] === item
;
10522 if ( this.items
[i
].isSelected() !== selected
) {
10523 this.items
[i
].setSelected( selected
);
10528 this.emit( 'select', item
);
10537 * @param {OO.ui.OptionWidget} [item] Item to press, omit to depress all
10541 OO
.ui
.SelectWidget
.prototype.pressItem = function ( item
) {
10542 var i
, len
, pressed
,
10545 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10546 pressed
= this.items
[i
] === item
;
10547 if ( this.items
[i
].isPressed() !== pressed
) {
10548 this.items
[i
].setPressed( pressed
);
10553 this.emit( 'press', item
);
10562 * Identical to #selectItem, but may vary in subclasses that want to take additional action when
10563 * an item is selected using the keyboard or mouse.
10565 * @param {OO.ui.OptionWidget} item Item to choose
10569 OO
.ui
.SelectWidget
.prototype.chooseItem = function ( item
) {
10570 this.selectItem( item
);
10571 this.emit( 'choose', item
);
10577 * Get an item relative to another one.
10579 * @param {OO.ui.OptionWidget} item Item to start at
10580 * @param {number} direction Direction to move in
10581 * @return {OO.ui.OptionWidget|null} Item at position, `null` if there are no items in the menu
10583 OO
.ui
.SelectWidget
.prototype.getRelativeSelectableItem = function ( item
, direction
) {
10584 var inc
= direction
> 0 ? 1 : -1,
10585 len
= this.items
.length
,
10586 index
= item
instanceof OO
.ui
.OptionWidget
?
10587 $.inArray( item
, this.items
) : ( inc
> 0 ? -1 : 0 ),
10588 stopAt
= Math
.max( Math
.min( index
, len
- 1 ), 0 ),
10590 // Default to 0 instead of -1, if nothing is selected let's start at the beginning
10591 Math
.max( index
, -1 ) :
10592 // Default to n-1 instead of -1, if nothing is selected let's start at the end
10593 Math
.min( index
, len
);
10596 i
= ( i
+ inc
+ len
) % len
;
10597 item
= this.items
[i
];
10598 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10601 // Stop iterating when we've looped all the way around
10602 if ( i
=== stopAt
) {
10610 * Get the next selectable item.
10612 * @return {OO.ui.OptionWidget|null} Item, `null` if ther aren't any selectable items
10614 OO
.ui
.SelectWidget
.prototype.getFirstSelectableItem = function () {
10617 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
10618 item
= this.items
[i
];
10619 if ( item
instanceof OO
.ui
.OptionWidget
&& item
.isSelectable() ) {
10630 * When items are added with the same values as existing items, the existing items will be
10631 * automatically removed before the new items are added.
10633 * @param {OO.ui.OptionWidget[]} items Items to add
10634 * @param {number} [index] Index to insert items after
10638 OO
.ui
.SelectWidget
.prototype.addItems = function ( items
, index
) {
10639 var i
, len
, item
, hash
,
10642 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10644 hash
= OO
.getHash( item
.getData() );
10645 if ( hash
in this.hashes
) {
10646 // Remove item with same value
10647 remove
.push( this.hashes
[hash
] );
10649 this.hashes
[hash
] = item
;
10651 if ( remove
.length
) {
10652 this.removeItems( remove
);
10656 OO
.ui
.GroupWidget
.prototype.addItems
.call( this, items
, index
);
10658 // Always provide an index, even if it was omitted
10659 this.emit( 'add', items
, index
=== undefined ? this.items
.length
- items
.length
- 1 : index
);
10667 * Items will be detached, not removed, so they can be used later.
10669 * @param {OO.ui.OptionWidget[]} items Items to remove
10673 OO
.ui
.SelectWidget
.prototype.removeItems = function ( items
) {
10674 var i
, len
, item
, hash
;
10676 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10678 hash
= OO
.getHash( item
.getData() );
10679 if ( hash
in this.hashes
) {
10680 // Remove existing item
10681 delete this.hashes
[hash
];
10683 if ( item
.isSelected() ) {
10684 this.selectItem( null );
10689 OO
.ui
.GroupWidget
.prototype.removeItems
.call( this, items
);
10691 this.emit( 'remove', items
);
10699 * Items will be detached, not removed, so they can be used later.
10704 OO
.ui
.SelectWidget
.prototype.clearItems = function () {
10705 var items
= this.items
.slice();
10710 OO
.ui
.GroupWidget
.prototype.clearItems
.call( this );
10711 this.selectItem( null );
10713 this.emit( 'remove', items
);
10719 * Select widget containing button options.
10721 * Use together with OO.ui.ButtonOptionWidget.
10724 * @extends OO.ui.SelectWidget
10727 * @param {Object} [config] Configuration options
10729 OO
.ui
.ButtonSelectWidget
= function OoUiButtonSelectWidget( config
) {
10730 // Parent constructor
10731 OO
.ui
.ButtonSelectWidget
.super.call( this, config
);
10734 this.$element
.addClass( 'oo-ui-buttonSelectWidget' );
10739 OO
.inheritClass( OO
.ui
.ButtonSelectWidget
, OO
.ui
.SelectWidget
);
10742 * Overlaid menu of options.
10744 * Menus are clipped to the visible viewport. They do not provide a control for opening or closing
10747 * Use together with OO.ui.MenuItemWidget.
10750 * @extends OO.ui.SelectWidget
10751 * @mixins OO.ui.ClippableElement
10754 * @param {Object} [config] Configuration options
10755 * @cfg {OO.ui.InputWidget} [input] Input to bind keyboard handlers to
10756 * @cfg {OO.ui.Widget} [widget] Widget to bind mouse handlers to
10757 * @cfg {boolean} [autoHide=true] Hide the menu when the mouse is pressed outside the menu
10759 OO
.ui
.MenuWidget
= function OoUiMenuWidget( config
) {
10760 // Config intialization
10761 config
= config
|| {};
10763 // Parent constructor
10764 OO
.ui
.MenuWidget
.super.call( this, config
);
10766 // Mixin constructors
10767 OO
.ui
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
10770 this.flashing
= false;
10771 this.visible
= false;
10772 this.newItems
= null;
10773 this.autoHide
= config
.autoHide
=== undefined || !!config
.autoHide
;
10774 this.$input
= config
.input
? config
.input
.$input
: null;
10775 this.$widget
= config
.widget
? config
.widget
.$element
: null;
10776 this.$previousFocus
= null;
10777 this.isolated
= !config
.input
;
10778 this.onKeyDownHandler
= OO
.ui
.bind( this.onKeyDown
, this );
10779 this.onDocumentMouseDownHandler
= OO
.ui
.bind( this.onDocumentMouseDown
, this );
10784 .attr( 'role', 'menu' )
10785 .addClass( 'oo-ui-menuWidget' );
10790 OO
.inheritClass( OO
.ui
.MenuWidget
, OO
.ui
.SelectWidget
);
10791 OO
.mixinClass( OO
.ui
.MenuWidget
, OO
.ui
.ClippableElement
);
10796 * Handles document mouse down events.
10798 * @param {jQuery.Event} e Key down event
10800 OO
.ui
.MenuWidget
.prototype.onDocumentMouseDown = function ( e
) {
10801 if ( !$.contains( this.$element
[0], e
.target
) && ( !this.$widget
|| !$.contains( this.$widget
[0], e
.target
) ) ) {
10802 this.toggle( false );
10807 * Handles key down events.
10809 * @param {jQuery.Event} e Key down event
10811 OO
.ui
.MenuWidget
.prototype.onKeyDown = function ( e
) {
10814 highlightItem
= this.getHighlightedItem();
10816 if ( !this.isDisabled() && this.isVisible() ) {
10817 if ( !highlightItem
) {
10818 highlightItem
= this.getSelectedItem();
10820 switch ( e
.keyCode
) {
10821 case OO
.ui
.Keys
.ENTER
:
10822 this.chooseItem( highlightItem
);
10825 case OO
.ui
.Keys
.UP
:
10826 nextItem
= this.getRelativeSelectableItem( highlightItem
, -1 );
10829 case OO
.ui
.Keys
.DOWN
:
10830 nextItem
= this.getRelativeSelectableItem( highlightItem
, 1 );
10833 case OO
.ui
.Keys
.ESCAPE
:
10834 if ( highlightItem
) {
10835 highlightItem
.setHighlighted( false );
10837 this.toggle( false );
10843 this.highlightItem( nextItem
);
10844 nextItem
.scrollElementIntoView();
10848 e
.preventDefault();
10849 e
.stopPropagation();
10856 * Bind key down listener.
10858 OO
.ui
.MenuWidget
.prototype.bindKeyDownListener = function () {
10859 if ( this.$input
) {
10860 this.$input
.on( 'keydown', this.onKeyDownHandler
);
10862 // Capture menu navigation keys
10863 this.getElementWindow().addEventListener( 'keydown', this.onKeyDownHandler
, true );
10868 * Unbind key down listener.
10870 OO
.ui
.MenuWidget
.prototype.unbindKeyDownListener = function () {
10871 if ( this.$input
) {
10872 this.$input
.off( 'keydown' );
10874 this.getElementWindow().removeEventListener( 'keydown', this.onKeyDownHandler
, true );
10881 * This will close the menu when done, unlike selectItem which only changes selection.
10883 * @param {OO.ui.OptionWidget} item Item to choose
10886 OO
.ui
.MenuWidget
.prototype.chooseItem = function ( item
) {
10890 OO
.ui
.MenuWidget
.super.prototype.chooseItem
.call( this, item
);
10892 if ( item
&& !this.flashing
) {
10893 this.flashing
= true;
10894 item
.flash().done( function () {
10895 widget
.toggle( false );
10896 widget
.flashing
= false;
10899 this.toggle( false );
10908 OO
.ui
.MenuWidget
.prototype.addItems = function ( items
, index
) {
10912 OO
.ui
.MenuWidget
.super.prototype.addItems
.call( this, items
, index
);
10915 if ( !this.newItems
) {
10916 this.newItems
= [];
10919 for ( i
= 0, len
= items
.length
; i
< len
; i
++ ) {
10921 if ( this.isVisible() ) {
10922 // Defer fitting label until item has been attached
10925 this.newItems
.push( item
);
10929 // Reevaluate clipping
10938 OO
.ui
.MenuWidget
.prototype.removeItems = function ( items
) {
10940 OO
.ui
.MenuWidget
.super.prototype.removeItems
.call( this, items
);
10942 // Reevaluate clipping
10951 OO
.ui
.MenuWidget
.prototype.clearItems = function () {
10953 OO
.ui
.MenuWidget
.super.prototype.clearItems
.call( this );
10955 // Reevaluate clipping
10964 OO
.ui
.MenuWidget
.prototype.toggle = function ( visible
) {
10965 visible
= ( visible
=== undefined ? !this.visible
: !!visible
) && !!this.items
.length
;
10968 change
= visible
!== this.isVisible();
10971 OO
.ui
.MenuWidget
.super.prototype.toggle
.call( this, visible
);
10975 this.bindKeyDownListener();
10977 // Change focus to enable keyboard navigation
10978 if ( this.isolated
&& this.$input
&& !this.$input
.is( ':focus' ) ) {
10979 this.$previousFocus
= this.$( ':focus' );
10980 this.$input
[0].focus();
10982 if ( this.newItems
&& this.newItems
.length
) {
10983 for ( i
= 0, len
= this.newItems
.length
; i
< len
; i
++ ) {
10984 this.newItems
[i
].fitLabel();
10986 this.newItems
= null;
10988 this.toggleClipping( true );
10991 if ( this.autoHide
) {
10992 this.getElementDocument().addEventListener(
10993 'mousedown', this.onDocumentMouseDownHandler
, true
10997 this.unbindKeyDownListener();
10998 if ( this.isolated
&& this.$previousFocus
) {
10999 this.$previousFocus
[0].focus();
11000 this.$previousFocus
= null;
11002 this.getElementDocument().removeEventListener(
11003 'mousedown', this.onDocumentMouseDownHandler
, true
11005 this.toggleClipping( false );
11013 * Menu for a text input widget.
11015 * This menu is specially designed to be positioned beneath the text input widget. Even if the input
11016 * is in a different frame, the menu's position is automatically calulated and maintained when the
11017 * menu is toggled or the window is resized.
11020 * @extends OO.ui.MenuWidget
11023 * @param {OO.ui.TextInputWidget} input Text input widget to provide menu for
11024 * @param {Object} [config] Configuration options
11025 * @cfg {jQuery} [$container=input.$element] Element to render menu under
11027 OO
.ui
.TextInputMenuWidget
= function OoUiTextInputMenuWidget( input
, config
) {
11028 // Parent constructor
11029 OO
.ui
.TextInputMenuWidget
.super.call( this, config
);
11032 this.input
= input
;
11033 this.$container
= config
.$container
|| this.input
.$element
;
11034 this.onWindowResizeHandler
= OO
.ui
.bind( this.onWindowResize
, this );
11037 this.$element
.addClass( 'oo-ui-textInputMenuWidget' );
11042 OO
.inheritClass( OO
.ui
.TextInputMenuWidget
, OO
.ui
.MenuWidget
);
11047 * Handle window resize event.
11049 * @param {jQuery.Event} e Window resize event
11051 OO
.ui
.TextInputMenuWidget
.prototype.onWindowResize = function () {
11058 OO
.ui
.TextInputMenuWidget
.prototype.toggle = function ( visible
) {
11059 visible
= !!visible
;
11061 var change
= visible
!== this.isVisible();
11064 OO
.ui
.TextInputMenuWidget
.super.prototype.toggle
.call( this, visible
);
11067 if ( this.isVisible() ) {
11069 this.$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
11071 this.$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
11078 * Position the menu.
11082 OO
.ui
.TextInputMenuWidget
.prototype.position = function () {
11084 $container
= this.$container
,
11085 dimensions
= $container
.offset();
11087 // Position under input
11088 dimensions
.top
+= $container
.height();
11090 // Compensate for frame position if in a differnt frame
11091 if ( this.input
.$.$iframe
&& this.input
.$.context
!== this.$element
[0].ownerDocument
) {
11092 frameOffset
= OO
.ui
.Element
.getRelativePosition(
11093 this.input
.$.$iframe
, this.$element
.offsetParent()
11095 dimensions
.left
+= frameOffset
.left
;
11096 dimensions
.top
+= frameOffset
.top
;
11098 // Fix for RTL (for some reason, no need to fix if the frameoffset is set)
11099 if ( this.$element
.css( 'direction' ) === 'rtl' ) {
11100 dimensions
.right
= this.$element
.parent().position().left
-
11101 $container
.width() - dimensions
.left
;
11102 // Erase the value for 'left':
11103 delete dimensions
.left
;
11106 this.$element
.css( dimensions
);
11107 this.setIdealSize( $container
.width() );
11113 * Structured list of items.
11115 * Use with OO.ui.OutlineItemWidget.
11118 * @extends OO.ui.SelectWidget
11121 * @param {Object} [config] Configuration options
11123 OO
.ui
.OutlineWidget
= function OoUiOutlineWidget( config
) {
11124 // Config intialization
11125 config
= config
|| {};
11127 // Parent constructor
11128 OO
.ui
.OutlineWidget
.super.call( this, config
);
11131 this.$element
.addClass( 'oo-ui-outlineWidget' );
11136 OO
.inheritClass( OO
.ui
.OutlineWidget
, OO
.ui
.SelectWidget
);
11139 * Switch that slides on and off.
11142 * @extends OO.ui.Widget
11143 * @mixins OO.ui.ToggleWidget
11146 * @param {Object} [config] Configuration options
11147 * @cfg {boolean} [value=false] Initial value
11149 OO
.ui
.ToggleSwitchWidget
= function OoUiToggleSwitchWidget( config
) {
11150 // Parent constructor
11151 OO
.ui
.ToggleSwitchWidget
.super.call( this, config
);
11153 // Mixin constructors
11154 OO
.ui
.ToggleWidget
.call( this, config
);
11157 this.dragging
= false;
11158 this.dragStart
= null;
11159 this.sliding
= false;
11160 this.$glow
= this.$( '<span>' );
11161 this.$grip
= this.$( '<span>' );
11164 this.$element
.on( 'click', OO
.ui
.bind( this.onClick
, this ) );
11167 this.$glow
.addClass( 'oo-ui-toggleSwitchWidget-glow' );
11168 this.$grip
.addClass( 'oo-ui-toggleSwitchWidget-grip' );
11170 .addClass( 'oo-ui-toggleSwitchWidget' )
11171 .append( this.$glow
, this.$grip
);
11176 OO
.inheritClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.Widget
);
11177 OO
.mixinClass( OO
.ui
.ToggleSwitchWidget
, OO
.ui
.ToggleWidget
);
11182 * Handle mouse down events.
11184 * @param {jQuery.Event} e Mouse down event
11186 OO
.ui
.ToggleSwitchWidget
.prototype.onClick = function ( e
) {
11187 if ( !this.isDisabled() && e
.which
=== 1 ) {
11188 this.setValue( !this.value
);